/
solutions.py
1446 lines (1224 loc) · 43.4 KB
/
solutions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!usr/bin/env python
import argparse
import util
def problem1():
"""
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
"""
total = 0
for n in xrange(1000):
if (n % 3 == 0) or (n % 5 == 0):
total += n
return total
def problem2():
"""
Each new term in the Fibonacci sequence is generated by adding the
previous two terms.
By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.
"""
total = 0
t0, t1, t2 = 0, 1, 0
while t0 <= 4000000:
if t0 % 2 == 0:
total += t0
t2, t1 = t1, t0
t0 = t2 + t1
return total
def problem3():
"""
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
"""
from math import floor, ceil, sqrt
number = 600851475143
def factor(num):
for n in xrange(2, int(ceil(sqrt(num)))):
if num % n == 0:
return n, num / n
return None, num
f1, f2 = 1, number
while f1 is not None:
f1, f2 = factor(f2)
return f2
def problem4():
"""
A palindromic number reads the same both ways. The largest palindrome
made from the product of two 2-digit numbers is 9009 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers.
"""
def is_palindrome(num):
num = str(num)
digits = len(num)
for i in xrange(int(digits / 2 + 1)):
if num[i] != num[digits - i - 1]:
return False
else:
return True
best = 0
for i in xrange(100, 1000):
for j in xrange(i, 1000):
if is_palindrome(i * j):
best = max(best, i * j)
return best
def problem5():
"""
2520 is the smallest number that can be divided by each of the numbers
from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of
the numbers from 1 to 20?
"""
i = 20
while True:
for j in xrange(1, 21):
if i % j != 0:
break
else:
return i
i += 20
return i
def problem6():
"""
Find the difference between the sum of the squares of the first one
hundred natural numbers and the square of the sum.
"""
sum_of_sqr = (100 * 101 * 201) / 6
sqr_of_sum = (100 * (100 + 1) / 2) ** 2
return abs(sum_of_sqr - sqr_of_sum)
def problem7():
"""
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can
see that the 6th prime is 13.
What is the 10 001st prime number?
"""
primes = [2, 3, 5, 7, 11, 13, 17, 19]
i = 21
while len(primes) < 10001:
for p in primes:
if i % p == 0:
break
else:
primes.append(i)
i += 2
return primes[-1]
def problem8():
"""
Find the greatest product of five consecutive digits in the given number.
"""
with open('files/8/number.txt', 'r') as f:
number = f.read().strip()
product = 0
for i in xrange(len(number) - 4):
digits = [int(char) for char in number[i:i+5]]
product = max(product, reduce(lambda x,y: x*y, digits))
return product
def problem9():
"""
A Pythagorean triplet is a set of three natural numbers, a < b < c, for
which, a^2 + b^2 = c^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
"""
for a in xrange(1, 334):
for b in xrange(a, 667):
c = 1000 - a - b
if a**2 + b**2 == c**2:
return a * b * c
return None
def problem10():
"""
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
"""
limit = 2000000
primes, primeset = util.primes(limit)
return sum(primes)
def problem11():
"""
What is the greatest product of four adjacent numbers in the same
direction (up, down, left, right, or diagonally) in the 20x20 grid?
"""
grid = [
map(int, "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08".split(' ')),
map(int, "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00".split(' ')),
map(int, "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65".split(' ')),
map(int, "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91".split(' ')),
map(int, "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80".split(' ')),
map(int, "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50".split(' ')),
map(int, "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70".split(' ')),
map(int, "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21".split(' ')),
map(int, "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72".split(' ')),
map(int, "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95".split(' ')),
map(int, "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92".split(' ')),
map(int, "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57".split(' ')),
map(int, "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58".split(' ')),
map(int, "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40".split(' ')),
map(int, "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66".split(' ')),
map(int, "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69".split(' ')),
map(int, "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36".split(' ')),
map(int, "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16".split(' ')),
map(int, "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54".split(' ')),
map(int, "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48".split(' ')),
]
def rows(grid):
return iter(grid)
def cols(grid):
for i in xrange(len(grid[0])):
yield [row[i] for row in grid]
def diags(grid):
for x in xrange(len(grid[0])):
yield [grid[y][xi] for xi, y in zip(xrange(x, len(grid[0])), xrange(0, len(grid) - x))]
yield [grid[y][xi] for xi, y in zip(xrange(x, len(grid[0])), xrange(len(grid)-1, x-1, -1))]
for y in xrange(1, len(grid)):
yield [grid[yi][x] for x, yi in zip(xrange(0, len(grid[0]) - y), xrange(y, len(grid)))]
yield [grid[yi][x] for x, yi in zip(xrange(0, len(grid[0]) - y), xrange(len(grid)-y-1, -1, -1))]
def products(L):
if len(L) >= 4:
for i in xrange(len(L) - 3):
yield reduce(lambda x,y: x*y, L[i:i+4])
best = 0
for row in rows(grid):
p = list(products(row))
if len(p) > 0:
best = max(best, *p)
for col in cols(grid):
p = list(products(col))
if len(p) > 0:
best = max(best, *p)
for diag in diags(grid):
p = list(products(diag))
if len(p) > 0:
best = max(best, *p)
return best
def problem12():
"""
The sequence of triangle numbers is generated by adding the natural numbers.
What is the first triangle number to have over five hundred divisors?
"""
from math import sqrt
def triangles():
total, i = 1, 1
while True:
yield total
i += 1
total += i
for x in triangles():
divisors = 2 # 1 and x
for i in xrange(2, int(sqrt(x)+1)):
if x % i == 0:
if x / i == i:
divisors += 1
else:
divisors += 2
if divisors > 500:
return x
def problem13():
"""
Work out the first ten digits of the sum of the following one-hundred
50-digit numbers.
"""
with open('files/13/numbers.txt', 'r') as f:
numbers = [int(line.strip()) for line in f]
return str(sum(numbers))[:10]
def problem14():
"""
The following iterative sequence is defined for the set of positive integers:
n => n/2 (n is even)
n => 3n + 1 (n is odd)
Which starting number, under one million, produces the longest chain?
"""
def seq(x):
yield x
while x > 1:
x = x/2 if x % 2 == 0 else 3*x + 1
yield x
best = 1, 1
for i in xrange(1, 1000000):
pathlen = sum(1 for _ in seq(i))
if pathlen > best[1]:
best = i, pathlen
return best[0]
def problem15():
"""
Starting in the top left corner of a 2x2 grid, and only being able to move
to the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20x20 grid?
"""
dimension = 20,20
grid = [[0 for x in xrange(dimension[0]+1)] for y in xrange(dimension[1]+1)]
for x in xrange(dimension[0]+1):
grid[x][0] = 1
for y in xrange(dimension[1]+1):
grid[0][y] = 1
for i in xrange(1, dimension[0]+1):
for j in xrange(1, dimension[1]+1):
grid[i][j] = grid[i-1][j] + grid[i][j-1]
return grid[dimension[0]][dimension[1]]
def problem16():
"""
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?
"""
return sum(util.digits(2**1000))
def problem17():
"""
If the numbers 1 to 5 are written out in words: one, two, three, four,
five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written
out in words, how many letters would be used?
"""
ones = [
'', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten',
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen'
]
tens = [
'', '', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety'
]
total = 0
for i in xrange(1, 1000):
word = ''
if i >= 100:
word += ones[i/100] + 'hundred'
if i%100 != 0:
word += 'and'
if i%100 < 20:
word += ones[i%100]
else:
word += tens[i/10%10] + ones[i%10]
total += len(word)
return total + 11 # to include 1000
def problem18():
"""
By starting at the top of the triangle below and moving to adjacent
numbers on the row below, the maximum total from top to bottom is 23.
Find the maximum total from top to bottom of the triangle below:
"""
triangle = [
[75],
[95, 64],
[17, 47, 82],
[18, 35, 87, 10],
[20, 4, 82, 47, 65],
[19, 1, 23, 75, 3, 34],
[88, 2, 77, 73, 7, 63, 67],
[99, 65, 04, 28, 6, 16, 70, 92],
[41, 41, 26, 56, 83, 40, 80, 70, 33],
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
[63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
[04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]
]
# Destructive dynamic programming, bubbles up max path length
depth = len(triangle)-2
while depth >= 0:
row = triangle[depth]
children = triangle[depth+1]
for i in xrange(len(row)):
row[i] += max(children[i], children[i+1])
depth -= 1
return triangle[0][0]
def problem19():
"""
You are given the following information, but you may prefer to do some
research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a
century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
"""
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap_year(year):
if year % 100 == 0:
return year % 400 == 0
else:
return year % 4 == 0
def is_sunday(day, month, year): # [0-max], [0-11], YYYY
day_offset = 1 # sequence starts on a monday
for nyear in xrange(1900, year):
day_offset += 366 if is_leap_year(nyear) else 365
if is_leap_year(year):
day_offset += sum(leap[:month]) + day
else:
day_offset += sum(months[:month]) + day
return day_offset % 7 == 0
sundays = 0
for year in xrange(1901, 2001):
for month in xrange(0, 12):
if is_sunday(0, month, year):
sundays += 1
return sundays
def problem20():
"""
Find the sum of the digits in the number 100!
"""
n = reduce(lambda x,y: x*y, xrange(1, 101))
return sum(util.digits(n))
def problem21():
"""
Let d(n) be defined as the sum of proper divisors of n (numbers less than
n which divide evenly into n). If d(a) = b and d(b) = a, where a != b, then
a and b are an amicable pair and each of a and b are called amicable
numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22,
44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1,
2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
"""
limit = 10000
sieve = [[] for i in xrange(limit)]
total = 0
for i in xrange(1, limit):
if sieve[i]:
pair = sum(sieve[i]), i
if pair[0] < pair[1] and sum(sieve[pair[0]]) == pair[1]:
total += pair[0] + pair[1]
for j in xrange(2 * i, limit, i):
sieve[j].append(i)
return total
def problem22():
"""
Using names.txt, a 46K text file containing over five-thousand first
names, begin by sorting it into alphabetical order. Then working out
the alphabetical value for each name, multiply this value by its
alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN,
which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.
So, COLIN would obtain a score of 938 x 53 = 49714.
What is the total of all the name scores in the file?
"""
with open('files/22/names.txt', 'r') as f:
names = []
for name in f.read().strip().split(','):
names.append(name.strip('"'))
names.sort()
def score(name):
ascii_offset = 96
name = list(name.lower())
return sum(map(lambda c: ord(c) - ascii_offset, name))
total = 0
for i in xrange(len(names)):
total += score(names[i]) * (i+1)
return total
def problem23():
"""
A perfect number is a number for which the sum of its proper divisors is
exactly equal to the number. For example, the sum of the proper divisors
of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect
number.
A number n is called deficient if the sum of its proper divisors is less
than n and it is called abundant if this sum exceeds n.
Find the sum of all the positive integers which cannot be written as the
sum of two abundant numbers.
"""
limit = 28123
sieve = [[] for i in xrange(limit)]
abundant = set()
for i in xrange(1, limit):
if sieve[i] and sum(sieve[i]) > i:
abundant.add(i)
for j in xrange(2 * i, limit, i):
sieve[j].append(i)
total = 0
for i in xrange(1, limit):
for j in abundant:
if i - j in abundant:
break
else:
total += i
return total
def problem24():
"""
What is the millionth lexicographic permutation of the digits
0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
"""
limit = 1000000
permute = util.permutations('0123456789')
for j in xrange(limit - 1):
next(permute)
return next(permute)
def problem25():
"""
What is the first term in the Fibonacci sequence to contain 1000 digits?
"""
def fib():
f0, f1 = 0, 1
yield 1
while True:
yield f1 + f0
f1, f0 = f1 + f0, f1
for i, n in enumerate(fib()):
if len(str(n)) == 1000:
return i + 1
def problem26():
"""
Find the value of d < 1000 for which 1/d contains the longest recurring
cycle in its decimal fraction part.
"""
limit = 1000
best = 0, 0
def cycle(d):
remainders, r = {}, 1
length = 0
while r > 0 and r not in remainders:
remainders[r] = length
r = (r * 10) % d
length += 1
return length
for i in xrange(1, limit):
length = cycle(i)
if length > best[0]:
best = length, i
if best[0] >= limit:
break
return best[1] # Problem statement is find d, not cycle length
def problem27():
"""
Considering the quadratic formula n^2 + an + b where |a| < 1000 and
|b| < 1000, find the product of the coefficients, a and b, for the
quadratic expression that produces the maximum number of primes for
consecutive values of n, starting with n = 0.
"""
limit = 1000
primes, primeset = util.primes(limit)
def quad(a, b):
return lambda n: n**2 + a*n + b
max_n = 0
max_coef = 0, 0
for a in xrange(-1000, 1000):
for b in primes:
q = quad(a, b)
n = 0
while q(n) in primeset:
n += 1
if n > max_n:
max_n = n
max_coef = a, b
return max_coef[0] * max_coef[1]
def problem28():
"""
Starting with the number 1 and moving to the right in a clockwise
direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral
formed in the same way?
"""
size = 1001
def spiral_diag(n):
"""Generator of values in diagonals of a spiral using n numbers."""
i, step = 1, 2
yield i
while i < n:
for j in xrange(4):
i += step
yield i
step += 2
return sum(spiral_diag(size ** 2))
def problem29():
"""
How many distinct terms are in the sequence generated by ab for
2 <= a <= 100 and 2 <= b <= 100?
"""
terms = set()
for a in xrange(2, 101):
for b in xrange(2, 101):
terms.add(a ** b)
return len(terms)
def problem30():
"""
Find the sum of all the numbers that can be written as the sum of fifth
powers of their digits.
"""
powers = [0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049]
total, i = 0, 3
while i < 1000000:
num_total = 0
for d in util.digits(i):
num_total += powers[d]
if num_total == i:
total += i
i += 1
return total
def problem31():
"""
How many different ways can 2 pounds be made using any number of coins?
"""
coins = (1, 2, 5, 10, 20, 50, 100, 200)
amount = 200
table = [[0 for c in xrange(amount+1)] for i in xrange(len(coins))]
for c in xrange(amount+1):
for i in xrange(len(coins)):
if c - coins[i] < 0:
table[i][c] = table[i - 1][c]
elif c - coins[i] == 0:
table[i][c] = table[i - 1][c] + 1
else:
table[i][c] = table[i][c - coins[i]] + table[i - 1][c]
return table[-1][-1]
def problem32():
"""
We shall say that an n-digit number is pandigital if it makes use of all
the digits 1 to n exactly once; for example, the 5-digit number, 15234,
is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing
multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product
identity can be written as a 1 through 9 pandigital.
"""
product_set = set()
for p in util.permutations('123456789'):
for i in xrange(1, len(p) / 3 + 1):
for j in xrange(i + 1, len(p) / 3 * 2 + 1):
triplet = int(p[:i]), int(p[i:j]), int(p[j:])
if triplet[0] * triplet[1] == triplet[2]:
product_set.add(triplet[2])
return sum(product_set)
def problem33():
"""
The fraction 49/98 is a curious fraction, as an inexperienced
mathematician in attempting to simplify it may incorrectly believe
that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction,
less than one in value, and containing two digits in the numerator and
denominator.
If the product of these four fractions is given in its lowest common
terms, find the value of the denominator.
"""
from fractions import Fraction
keep = []
for numer in xrange(10, 100):
for denom in xrange(numer + 1, 100):
if (numer % 10 == 0 and denom % 10 == 0) or\
(numer % 11 == 0 and denom % 11 == 0):
continue
ndigits = [numer // 10, numer % 10]
ddigits = [denom // 10, denom % 10]
if ndigits[0] in ddigits:
ddigits.remove(ndigits[0])
ndigits.remove(ndigits[0])
elif ndigits[1] in ddigits:
ddigits.remove(ndigits[1])
ndigits.remove(ndigits[1])
else:
continue
if ddigits[0] != 0 and\
Fraction(ndigits[0], ddigits[0]) == Fraction(numer, denom):
keep.append(Fraction(numer, denom))
return reduce(lambda x,y: x*y, keep).denominator
def problem34():
"""
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial
of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
"""
facs = [1, 1, 2, 6, 24, 120, 720, 504, 40320, 362880]
total, i = 0, 3
while i < 1000000:
num_total = 0
for d in util.digits(i):
num_total += facs[d]
if num_total == i:
total += i
i += 1
return total
def problem35():
"""
The number, 197, is called a circular prime because all rotations of the
digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37,
71, 73, 79, and 97.
How many circular primes are there below one million?
"""
limit = 1000000
primes, primeset = util.primes(limit)
def rotations(digits):
for i in xrange(len(digits)):
yield digits[i:] + digits[:i]
count = 0
for i in xrange(2, limit):
if i in primeset:
for p in rotations(str(i)):
if int(p) not in primeset:
break
else:
count += 1
return count
def problem36():
"""
Find the sum of all numbers, less than one million, which are palindromic
in base 10 and base 2.
"""
limit = 1000000
total = 0
def is_palindrome(num_str):
for i in xrange(len(num_str) / 2):
if num_str[i] != num_str[-1 - i]:
return False
return True
for i in xrange(limit):
if is_palindrome(str(i)) and is_palindrome(bin(i)[2:]):
total += i
return total
def problem37():
"""
Find the sum of the only eleven primes that are both truncatable from
left to right and right to left.
"""
limit = 1000000
total = 0
primes, primeset = util.primes(limit)
def is_truncatable(i):
left = right = str(i)
while len(left) > 0 and len(right) > 0:
if int(left) not in primeset or int(right) not in primeset:
return False
else:
left, right = left[1:], right[:-1]
return True
for i in xrange(11, limit, 2):
if is_truncatable(i):
total += i
return total
def problem38():
"""
Take the number 192 and multiply it by each of 1, 2, and 3:
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576.
We will call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3,
4, and 5, giving the pandigital, 918273645, which is the concatenated
product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as
the concatenated product of an integer with (1,2, ... , n) where n > 1?
"""
limit = 10000
best = 0
def is_pandigital(num_str):
return ''.join(sorted(num_str)) == "123456789"[:len(num_str)]
for i in xrange(limit):
num_str = ""
for j in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
num_str += str(i * j)
if is_pandigital(num_str) and int(num_str) > best:
best = int(num_str)
break
elif len(num_str) > 9:
break
return best
def problem39():
"""
If p is the perimeter of a right angle triangle with integral length sides,
{a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p <= 1000, is the number of solutions maximised?
"""
limit = 1000
best = 0, 0
for p in xrange(1, limit + 1):
solutions = 0
for a in xrange(1, p/3 + 1):
for b in xrange(a + 1, p/2):
c = p - b - a
if a ** 2 + b ** 2 == c ** 2:
solutions += 1
if solutions > best[0]:
best = solutions, p
return best[1]
def problem40():
"""
An irrational decimal fraction is created by concatenating the positive
integers:
0.123456789101112131415161718192021...
It can be seen that the 12th digit of the fractional part is 1.
If dn represents the nth digit of the fractional part, find the value
of the following expression.
d1 x d10 x d100 x d1000 x d10000 x d100000 x d1000000
"""
d, i = "", 1
while len(d) <= 1000001:
d += str(i)
i += 1
digits = map(int, [d[0], d[9], d[99], d[999], d[9999], d[99999], d[999999]])
return reduce(lambda x,y: x*y, digits)
def problem41():
"""
We shall say that an n-digit number is pandigital if it makes use of
all the digits 1 to n exactly once. For example, 2143 is a 4-digit
pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
"""
from random import randint
def is_prime(n):
if pow(2, n, n) != 2:
return False
for i in xrange(50):
a = randint(2, n-1)
if util.gcd(n, a) == 1 and pow(a, n-1, n) != 1:
return False
return True
best = 2143
for j in xrange(1, 10):
for i in util.permutations(''.join(map(str, xrange(1, j+1)))):
i = int(i)
if i > best:
if is_prime(i):
best = i
return best
def problem42():
"""
The nth term of the sequence of triangle numbers is given by,
tn = (1/2)n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
By converting each letter in a word to a number corresponding to its
alphabetical position and adding these values we form a word value.
For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the
word value is a triangle number then we shall call the word a triangle
word.
How many of the words in words.txt are triangle words?
"""
with open('files/42/words.txt', 'r') as f:
words = []
for word in f.read().strip().split(','):
words.append(word.strip('"'))
score_count = {}
# Generates all triangles up to and including t_n
def triangles(n):
for i in xrange(1, n+1):
yield i * (i+1) / 2
def score(word):
total = 0
for c in word:
total += ord(c) - 96 # Ascii offset
return total
max_score = 0
for word in words:
word = word.strip('"').lower()
word_score = score(word)
if word_score > max_score:
max_score = word_score
if word_score in score_count:
score_count[word_score] += 1
else:
score_count[word_score] = 1
total = 0
for t in triangles(max_score):
if t in score_count:
total += score_count[t]
return total
def problem43():
"""
The number, 1406357289, is a 0 to 9 pandigital number because it is made
up of each of the digits 0 to 9 in some order, but it also has a rather
interesting sub-string divisibility property.
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
"""
factors = 2, 3, 5, 7, 11, 13, 17
total = 0
for digits in util.permutations('0123456789'):
for i in xrange(1, 8):
if int(digits[i:i+3]) % factors[i-1] != 0:
break
else:
total += int(digits)
return total