fork(3) download
  1. # Introduction:
  2.  
  3. # A deletable composite is a composite number with two or more digits
  4. # that remains composite when any digit is deleted. For example, 104
  5. # is a deletable composite because 10, 14, and 04 are composite.
  6. # Note that leading zeros are allowed.
  7. #
  8. # The following Python 2.7 code computes a list of all deletable composites
  9. # up to a given bound. It is intended as a supplement for Sequence A225619
  10. # in the On-line Encyclopedia of Integer Sequences. The sequence was authored
  11. # by Derek Orr.
  12. #
  13. # The code for the Sieve of Eratosthenes is copied from
  14. # http://stackoverflow.com/questions/1023768/sieve-of-atkin-explanation
  15. # but I cannot find the original author.
  16. #
  17. # All other code was written by David Radcliffe and is released to the public domain.
  18. # If you use this code, attribution is appreciated but not required.
  19.  
  20. from math import sqrt
  21.  
  22. N = 1000
  23. sqrtN = int(sqrt(N))+1
  24.  
  25. def sieveOfErat(end):
  26. if end < 2: return []
  27.  
  28. #The array doesn't need to include even numbers
  29. lng = ((end/2)-1+end%2)
  30.  
  31. # Create array and assume all numbers in array are prime
  32. sieve = [True]*(lng+1)
  33.  
  34. # In the following code, you're going to see some funky
  35. # bit shifting and stuff, this is just transforming i and j
  36. # so that they represent the proper elements in the array.
  37. # The transforming is not optimal, and the number of
  38. # operations involved can be reduced.
  39.  
  40. # Only go up to square root of the end
  41. for i in range(int(sqrt(end)) >> 1):
  42.  
  43. # Skip numbers that aren't marked as prime
  44. if not sieve[i]: continue
  45.  
  46. # Unmark all multiples of i, starting at i**2
  47. for j in range( (i*(i + 3) << 1) + 3, lng, (i << 1) + 3):
  48. sieve[j] = False
  49.  
  50. # Don't forget 2!
  51. primes = [2]
  52.  
  53. # Gather all the primes into a list, leaving out the composite numbers
  54. primes.extend([(i << 1) + 3 for i in range(lng) if sieve[i]])
  55.  
  56. return primes
  57.  
  58. primes = sieveOfErat(sqrtN)
  59.  
  60. # isprime(n) returns True if n is prime, and False if n is not prime.
  61. # The function assumes that the input is an integer.
  62. # If n is greater than N*N but it has no prime factor less than or equal to N,
  63. # then the function will return None, indicating that the function is unable
  64. # to determine if n is prime.
  65.  
  66. def isprime(n):
  67. if n <= sqrtN and n in primes:
  68. return True
  69. if any(n % p == 0 for p in primes):
  70. return False
  71. if n <= N:
  72. return True
  73.  
  74. # iscomposite(n) returns True if n is composite, and it returns False if n is prime
  75. # or if n is less than 4. A value of None indicates that the function is unable
  76. # to determine if n is composite.
  77.  
  78. def iscomposite(n):
  79. if n < 4 or isprime(n):
  80. return False
  81. if n <= N:
  82. return True
  83.  
  84. # Generator which returns the numbers obtained by n by deleting one digit.
  85. # For example, deleteOneDigit(123) yields 23, 13, and 12.
  86.  
  87. def deleteOneDigit(n):
  88. s = str(n)
  89. for k in xrange(len(s)):
  90. yield int(s[:k] + s[k+1:])
  91.  
  92. # This function returns True if n is a deletable composite number, False otherwise.
  93.  
  94. def deletableComposite(n):
  95. return iscomposite(n) and all(iscomposite(d) for d in deleteOneDigit(n))
  96.  
  97. # List all deletable composites up to N.
  98.  
  99. print filter(deletableComposite, xrange(10,N+1))
  100.  
Success #stdin #stdout 0.02s 7728KB
stdin
Standard input is empty
stdout
[44, 46, 48, 49, 64, 66, 68, 69, 84, 86, 88, 94, 96, 98, 99, 104, 106, 108, 120, 122, 124, 125, 126, 128, 140, 142, 144, 145, 146, 148, 150, 152, 154, 155, 156, 158, 160, 162, 164, 165, 166, 168, 180, 182, 184, 185, 186, 188, 204, 206, 208, 210, 212, 214, 215, 216, 218, 220, 221, 222, 224, 225, 226, 228, 240, 242, 244, 245, 246, 248, 250, 252, 254, 255, 256, 258, 260, 262, 264, 265, 266, 268, 270, 272, 274, 275, 276, 278, 280, 282, 284, 285, 286, 287, 288, 304, 306, 308, 309, 320, 322, 324, 325, 326, 328, 330, 332, 333, 334, 335, 336, 338, 339, 340, 342, 344, 345, 346, 348, 350, 352, 354, 355, 356, 358, 360, 362, 363, 364, 365, 366, 368, 369, 380, 382, 384, 385, 386, 388, 390, 392, 393, 394, 395, 396, 398, 399, 404, 406, 408, 420, 422, 424, 425, 426, 428, 440, 442, 444, 445, 446, 448, 450, 452, 454, 455, 456, 458, 460, 462, 464, 465, 466, 468, 469, 480, 482, 484, 485, 486, 488, 490, 492, 494, 495, 496, 498, 504, 506, 508, 510, 512, 514, 515, 516, 518, 520, 522, 524, 525, 526, 527, 528, 540, 542, 544, 545, 546, 548, 550, 551, 552, 554, 555, 556, 558, 560, 562, 564, 565, 566, 568, 570, 572, 574, 575, 576, 578, 580, 581, 582, 584, 585, 586, 588, 604, 606, 608, 609, 620, 622, 624, 625, 626, 628, 630, 632, 633, 634, 635, 636, 638, 639, 640, 642, 644, 645, 646, 648, 649, 650, 652, 654, 655, 656, 658, 660, 662, 663, 664, 665, 666, 668, 669, 680, 682, 684, 685, 686, 688, 690, 692, 693, 694, 695, 696, 698, 699, 704, 706, 708, 720, 722, 724, 725, 726, 728, 740, 742, 744, 745, 746, 748, 750, 752, 754, 755, 756, 758, 760, 762, 764, 765, 766, 768, 770, 772, 774, 775, 776, 777, 778, 780, 782, 784, 785, 786, 788, 804, 806, 808, 810, 812, 814, 815, 816, 818, 820, 822, 824, 825, 826, 828, 840, 842, 844, 845, 846, 848, 850, 851, 852, 854, 855, 856, 858, 860, 862, 864, 865, 866, 868, 870, 872, 874, 875, 876, 878, 880, 882, 884, 885, 886, 888, 904, 906, 908, 909, 910, 912, 914, 915, 916, 918, 920, 921, 922, 924, 925, 926, 928, 930, 932, 933, 934, 935, 936, 938, 939, 940, 942, 944, 945, 946, 948, 949, 950, 951, 952, 954, 955, 956, 958, 960, 962, 963, 964, 965, 966, 968, 969, 980, 981, 982, 984, 985, 986, 988, 990, 992, 993, 994, 995, 996, 998, 999]