-
Notifications
You must be signed in to change notification settings - Fork 0
/
p06x.py
1126 lines (961 loc) · 45.5 KB
/
p06x.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
""" Project Euler Problems 60-69
"""
import math
# import string
import common
# from timeit import default_timer as timer
# 60 Prime pair sets
# The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order
# the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime.
# The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
# Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
def check_prime_concatenations(prime1, prime2, prime_list):
str_prime1 = str(prime1)
str_prime2 = str(prime2)
status = True
prime_cat = int(str_prime1 + str_prime2)
if not common.is_in_ordered_list(prime_cat, prime_list):
status = False
prime_cat = int(str_prime2 + str_prime1)
if not common.is_in_ordered_list(prime_cat, prime_list):
status = False
return status
def check_prime_pairs(index, prime_set, prime_list):
status = True
for prime in prime_set:
if not check_prime_concatenations(prime, prime_list[index], prime_list):
status = False
return status
def min_prime_pair_set(prime_count):
if prime_count < 5:
prime_list = common.sieve_erathosthenes2(10**(2*prime_count))
min_prime_sum = 10**prime_count
else:
prime_list = common.sieve_erathosthenes2(10**8)
min_prime_sum = 10**8
min_prime_set = []
prime_index = 1 # No pair with 2 will work
while prime_list[prime_index] < min_prime_sum//prime_count:
prime_set = [prime_list[prime_index]]
prime_sum = prime_set[0]
next_prime_index = prime_index + 1
while prime_list[next_prime_index]+prime_sum < min_prime_sum:
if check_prime_pairs(next_prime_index, prime_set, prime_list):
prime_set.append(prime_list[next_prime_index])
prime_sum = sum(prime_set)
# if len(prime_set) >= 4:
# print(' ', len(prime_set), prime_set)
if prime_sum > min_prime_sum:
break
if len(prime_set) == prime_count:
if prime_sum < min_prime_sum:
min_prime_set = prime_set[:]
min_prime_sum = prime_sum
break
next_prime_index += 1
prime_index += 1
return min_prime_set, min_prime_sum
def check_prime_concatenations_mr(prime1, prime2):
str_prime1 = str(prime1)
str_prime2 = str(prime2)
status = True
prime_cat = int(str_prime1 + str_prime2)
if not common.is_prime_mr(prime_cat):
status = False
prime_cat = int(str_prime2 + str_prime1)
if not common.is_prime_mr(prime_cat):
status = False
return status
def check_prime_pairs_mr(prime1, prime_set):
status = True
for prime2 in prime_set:
if not check_prime_concatenations_mr(prime1, prime2):
status = False
return status
def min_prime_pair_set_mr(prime_count):
if prime_count < 5:
prime_list = common.sieve_erathosthenes2(10**prime_count)
min_prime_sum = 10**prime_count
else:
prime_list = common.sieve_erathosthenes2(10**6)
min_prime_sum = 10**9
prime_list_count = len(prime_list)
min_prime_set = []
prime_index = 1 # No pair with 2 will work
while prime_list[prime_index] < min_prime_sum//prime_count:
prime_set = [prime_list[prime_index]]
prime_sum = prime_set[0]
next_prime_index = prime_index + 1
while prime_list[next_prime_index]+prime_sum < min_prime_sum:
if check_prime_pairs_mr(prime_list[next_prime_index], prime_set):
prime_set.append(prime_list[next_prime_index])
prime_sum = sum(prime_set)
# if len(prime_set) >= 4:
# print(' ', len(prime_set), prime_set)
if prime_sum > min_prime_sum:
break
if len(prime_set) == prime_count:
if prime_sum < min_prime_sum:
min_prime_set = prime_set[:]
min_prime_sum = prime_sum
break
next_prime_index += 1
if next_prime_index == prime_list_count:
break
prime_index += 1
if prime_index == prime_list_count:
break
return min_prime_set, min_prime_sum
# 61 Cyclical figurate numbers
# Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers
# and are generated by the following formulae:
# Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, ...
# Square P4,n=n**2 1, 4, 9, 16, 25, ...
# Pentagonal P5,n=n(3n-1)/2 1, 5, 12, 22, 35, ...
# Hexagonal P6,n=n(2n-1) 1, 6, 15, 28, 45, ...
# Heptagonal P7,n=n(5n-3)/2 1, 7, 18, 34, 55, ...
# Octagonal P8,n=n(3n-2) 1, 8, 21, 40, 65, ...
# The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.
# The set is cyclic, in that the last two digits of each number is the first two digits of the next number
# (including the last number with the first).
# Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882),
# is represented by a different number in the set.
# This is the only set of 4-digit numbers with this property.
# Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type:
# triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.
def generate_polygonal_number_list(side_count, digit_count):
"""
Create a list of all polygonal numbers of digit_count length of a particular side_count.
:param side_count: polygonal side count, e.g. Triangle side_count = 3, Square side_count = 4, etc.
:param digit_count: number of digits in the numbers
:return: list of all polygonal numbers of the specified side_count with the specified digit_count
"""
# Trying out lambda functions
# formula = side_count formula
# f_index = formula to determine the indices of the numbers of the specified digit_count
if side_count == 3:
formula = lambda x: x*(x+1)//2
f_index = lambda x: 1+int((math.sqrt(8*x)-1)/2) # sqrt(8*x+1)-1)/2
elif side_count == 4:
formula = lambda x: x*x
f_index = lambda x: 1+int(math.sqrt(x-1)) # sqrt(x)
elif side_count == 5:
formula = lambda x: x*(3*x-1)//2
f_index = lambda x: 1+int((math.sqrt(24*x)+1)/6) # (sqrt(24*x+1)+1)/6
elif side_count == 6:
formula = lambda x: x*(2*x-1)
f_index = lambda x: 1+int((math.sqrt(8*x)+1)/4) # (sqrt(8*x+1)+1)/4
elif side_count == 7:
formula = lambda x: x*(5*x-3)//2
f_index = lambda x: 1+int((math.sqrt(40*x+8)+3)/10) # (sqrt(40*x+9)+3)/10
elif side_count == 8:
formula = lambda x: x*(3*x-2)
f_index = lambda x: 1+int((math.sqrt(3*x)+1)/3) # (sqrt(3*x+1)+1)/3
else:
formula = lambda x: 0
f_index = lambda x: 0
start_index = f_index(10**(digit_count-1))
stop_index = f_index(10**(digit_count))
number_list = [formula(i) for i in range(start_index, stop_index)]
return number_list
def cyclic_4digit_set(set_count):
"""
Find set of set_count 4-digit numbers, each a different polygonal type that form a cyclic set.
:param set_count: the count of numbers forming th cyclic set
:return: a list of tuples of the form (side_count, number) that form the cyclic set.
"""
# Generate the polygonal lists for triangle...octagonal numbers
polygonal_numbers = []
for index in range(6):
side_count = index + 3
polygonal_numbers.append(PrefixSuffixNumbersClass(side_count, generate_polygonal_number_list(side_count, 4)))
# Delete numbers that have single digit suffixes
for index in range(6):
for num, prefix, suffix in reversed(polygonal_numbers[index]):
if suffix < 10:
polygonal_numbers[index].delete_number(num)
# Cyclic sets require suffixes to be matched to prefixes.
# Delete numbers that have prefixes that have no matching suffix on a different polygonal type
# Delete numbers that have suffixes that have no matching prefix on a different polygonal type
deleted = True
while deleted:
deleted = False
for index in range(6):
other_indices = [i for i in range(6)]
other_indices.remove(index)
for num, prefix, suffix in reversed(polygonal_numbers[index]):
prefix_total = 0
suffix_total = 0
for index2 in other_indices:
prefix_count, suffix_count = polygonal_numbers[index2].prefix_suffix_counts(prefix)
suffix_total += suffix_count
prefix_count, suffix_count = polygonal_numbers[index2].prefix_suffix_counts(suffix)
prefix_total += prefix_count
if (prefix_total == 0) or (suffix_total == 0):
deleted = True
polygonal_numbers[index].delete_number(num)
# Find cyclic sets
cyclic_set = []
for num, prefix, suffix in polygonal_numbers[-1]:
side_list = polygonal_numbers[:-1]
set_list = [(8, num)] # start with octagonal number because there are the fewest octagonal 4-digit numbers
# then find chains going forward and backward from that number.
prefix_suffix_list = add_node_prefix_suffix(4, side_list, suffix, set_list) # find 4-number prefix-suffix chains
suffix_prefix_list = add_node_suffix_prefix(4, side_list, prefix, set_list) # find 4-number suffix-prefix chains
# look for matches between last number of forward list and last number of backward list in order to make cyclic
for chain_prefix_suffix in prefix_suffix_list:
for chain_suffix_prefix in suffix_prefix_list:
if chain_prefix_suffix[-1] == chain_suffix_prefix[-1]:
# check that each number represents a different polygonal type
sides = []
for node in chain_prefix_suffix:
sides.append(node[0])
for node in chain_suffix_prefix[1:-1]:
if node[0] not in sides:
sides.append(node[0])
if len(sides) > 5:
cyclic_set.append(chain_prefix_suffix[:] + list(reversed(chain_suffix_prefix[1:-1])))
return cyclic_set
def add_node_prefix_suffix(remaining_count, remaining_side_list, pref, set_list):
"""
Recursive function to find prefix-suffix chains of set length with each number of a different polygonal type.
:param remaining_count: count of numbers remaining to be found
:param remaining_side_list: list of polygonal types not yet used and still available
:param pref: prefix to be found
:param set_list: list of numbers already in chain
:return: list of chains from initial number in set_list
"""
remaining_count -= 1
return_list = []
if remaining_count > 0:
for index in range(len(remaining_side_list)):
side_list = remaining_side_list[:index] + remaining_side_list[index+1:]
for num, prefix, suffix in remaining_side_list[index]:
if prefix == pref: # number[2] == suffix
return_list.extend(add_node_prefix_suffix(remaining_count, side_list, suffix, set_list+[(remaining_side_list[index].get_side_count(), num)]))
else:
if len(set_list) < 2:
return_list = []
else:
return_list = [set_list]
return return_list
def add_node_suffix_prefix(remaining_count, remaining_side_list, suff, set_list):
"""Recursive function to find suffix-prefix chains of set length with each number of a different polygonal type.
:param remaining_count: count of numbers remaining to be found
:param remaining_side_list: list of polygonal types not yet used and still available
:param suff: suffix to be found
:param set_list: list of numbers already in chain
:return: list of chains from initial number in set_list
"""
remaining_count -= 1
return_list = []
if remaining_count > 0:
for index in range(len(remaining_side_list)):
side_list = remaining_side_list[:index] + remaining_side_list[index+1:]
for num, prefix, suffix in remaining_side_list[index]:
if suffix == suff: # number[2] == suffix
return_list.extend(add_node_suffix_prefix(remaining_count, side_list, prefix,
set_list+[(remaining_side_list[index].get_side_count(), num)]))
else:
if len(set_list) < 2:
return_list = []
else:
return_list = [set_list]
return return_list
class PrefixSuffixNumbersClass:
"""Packages 4-digit numbers with their 2-digit prefixes and suffixes to simplify parameters
Keeps track of the polygonal side count, and length of list.
"""
def __init__(self, side_count, numberlist):
self.side_count = side_count
self.len = len(numberlist)
self.numberlist = [(numberlist[i], int(str(numberlist[i])[:2]), int(str(numberlist[i])[2:])) for i in range(self.len)]
def __iter__(self):
self.index = 0
return self
def __next__(self):
if self.index < self.len:
self.index += 1
return self.numberlist[self.index-1]
else:
raise StopIteration
def __reversed__(self):
return reversed(self.numberlist)
def __len__(self):
return self.len
def get_side_count(self):
return self.side_count
def delete_number(self, number):
for node in self.numberlist:
if node[0] == number:
self.numberlist.remove(node)
break
self.len = len(self.numberlist)
def prefix_suffix_counts(self, target):
prefix_count = 0
suffix_count = 0
for number, prefix, suffix in self.numberlist:
if prefix == target:
prefix_count += 1
if suffix == target:
suffix_count += 1
return prefix_count, suffix_count
# 62 Cubic permutations
# The cube, 41063625 (345**3), can be permuted to produce two other cubes: 56623104 (384**3) and 66430125 (405**3).
# In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
# Find the smallest cube for which exactly five permutations of its digits are cube.
def cube_permutations(perm_count):
"""Return the smallest set of perm_count cubes that are permutations of each other.
:param perm_count: the number of cube permutations to look for
:return: list of cube permutations of the first cube that has at least perm_count permutations
"""
found = False
perm_list = []
max_digit_count = 14
stop_base = 3
for digit_count in range(2, max_digit_count+1):
# first create list of cubes of the same length
start_base = stop_base
stop_base = int(math.pow(10**digit_count, 0.3333333333)) + 1
cube_list = [base**3 for base in range(start_base, stop_base)]
for cube in cube_list:
count = 0
perm_list = []
# count the number of permutations of cube
for cube2 in cube_list:
if common.is_permutation(str(cube), str(cube2)):
perm_list.append(cube2)
count += 1
if count >= perm_count:
found = True
break
if found:
break
if found:
break
if len(perm_list) >= perm_count:
return perm_list
else:
return []
# 63 Powerful digit counts
# The 5-digit number, 16807=7**5, is also a fifth power. Similarly, the 9-digit number, 134217728=8**9, is a ninth power.
# How many n-digit positive integers exist which are also an nth power?
def power_digit_counts():
count = 0
power = 1
while math.log10(9**power)+1 >= power: # log10(n)+1 gives the number of digits in n in base 10.
for base in range(1, 10):
if int(math.log10(base**power))+1 == power:
count += 1
power += 1
return count
# 64 Odd period square roots
# All square roots are periodic when written as continued fractions and can be written in the form:
# (see page)
# It can be seen that the sequence is repeating. For conciseness, we use the notation v23 = [4;(1,3,1,8)],
# to indicate that the block (1,3,1,8) repeats indefinitely.
# The first ten continued fraction representations of (irrational) square roots are:
# v2=[1;(2)], period=1
# v3=[1;(1,2)], period=2
# v5=[2;(4)], period=1
# v6=[2;(2,4)], period=2
# v7=[2;(1,1,1,4)], period=4
# v8=[2;(1,4)], period=2
# v10=[3;(6)], period=1
# v11=[3;(3,6)], period=2
# v12= [3;(2,6)], period=2
# v13=[3;(1,1,1,1,6)], period=5
# Exactly four continued fractions, for N <= 13, have an odd period.
# How many continued fractions for N <= 10000 have an odd period?
def continued_fraction_coeffs(number):
"""Return list of continuing fraction coefficients. ROUNDING ERRORS BEYOND SOME UNKNOWN NUMBER OF TERMS
:param number: Starting number for
:return:
"""
max_loop = 100
resolution = 10**6
repeating_sequence = []
non_repeating_sequence = [int(number)]
if int(number*resolution) != non_repeating_sequence[0]*resolution:
remainder_list = [1.0/(number - non_repeating_sequence[0])]
index = 0
while len(repeating_sequence)==0 and (index < max_loop):
index += 1
non_repeating_sequence.append(int(remainder_list[-1]))
remainder = (remainder_list[-1] - non_repeating_sequence[-1])
remainder_list.append(1.0/remainder)
remainder_int = int(remainder_list[-1]*resolution)
for index2 in range(index):
if int(remainder_list[index2]*resolution) == remainder_int:
repeating_sequence = non_repeating_sequence[index2+1:]
non_repeating_sequence = non_repeating_sequence[:index2+1]
break
non_repeating_sequence.append(tuple(repeating_sequence))
return non_repeating_sequence
def sqrt_continued_fraction_coeffs(number, max_term = 1000):
"""Return list of continuing fraction coefficients for sqrt of number. NO ROUNDING ERRORS.
For each term the following variables are calculated:
a(n) = int(1/a(n-1)_remainder)
a(n)_remainder = fractional part of a(n)
1/(a(n)_remainder = (sqrt(number)+b)/d
= a(n+1) + (sqrt(number)+c)/d
:param number: number for which sqrt(number) to be analyzed
:param max_term: maximum count of terms to be returned
:return: list of coefficients to be returned in the form
[a0, (a1, a2, a3)]
where a0 is non-repeating
and (a1, a2, a3) are repeating
"""
resolution = 10**6
sqrt_num = math.sqrt(number)
repeating_sequence = []
non_repeating_sequence = [int(sqrt_num)]
if int(sqrt_num*resolution) != int(sqrt_num)*resolution: # check for perfect squares
loop_input = sqrt_num
a = int(loop_input)
remainder = loop_input-a
remainder_p1 = 1/remainder # remainder of a(n+1)
remainder_p1 -= int(remainder_p1) # just take fractional portion
b = a
d = number-(a*a)
c = int(sqrt_num - d*(remainder_p1) + 0.1) # round this number, not just int
c_d_list = [(c, d)]
index = 0
while len(repeating_sequence)==0 and (index < max_term):
index += 1
loop_input = (sqrt_num + b) / d
a = int(loop_input)
non_repeating_sequence.append(a)
remainder = loop_input-a
remainder_p1 = 1/remainder # remainder of a(n+1)
remainder_p1 -= int(remainder_p1) # just take fractional portion
b = c
d = (number-(c*c))//d
c = int(sqrt_num - d*(remainder_p1) + 0.1) # round this number, not just int
c_d_list.append((c, d))
for index2 in range(index):
if c_d_list[index2] == c_d_list[index]:
repeating_sequence = non_repeating_sequence[index2+1:]
non_repeating_sequence = non_repeating_sequence[:index2+1]
break
non_repeating_sequence.append(tuple(repeating_sequence))
return non_repeating_sequence
def continued_fractions_term_counts(coeff_list):
"""
:param coeff_list:
:return:
"""
if isinstance(coeff_list[-1], tuple):
nonrepeating_count = len(coeff_list) - 1
repeating_count = len(coeff_list[-1])
if (repeating_count % 2) == 0:
odd_periodic = False
else:
odd_periodic = True
else:
nonrepeating_count = len(coeff_list)
repeating_count = 0
odd_periodic = False
return odd_periodic, nonrepeating_count, repeating_count
def continued_fractions_term_stats(function, start_n, max_n):
"""
:param function:
:param start_n:
:param max_n:
:return:
"""
one_nonrepeating_count = 0
zero_repeating_count = 0
zero_nonrepeating_count = 0
odd_periodic_count = 0
even_periodic_count = 0
for n in range(start_n, max_n+1):
coeff_list = function(n)
odd_periodic, nonrepeating_count, repeating_count = continued_fractions_term_counts(coeff_list)
if nonrepeating_count == 1 and repeating_count == 0:
one_nonrepeating_count += 1
if repeating_count == 0:
zero_repeating_count += 1
else:
if odd_periodic:
odd_periodic_count += 1
else:
even_periodic_count += 1
if nonrepeating_count == 0:
zero_nonrepeating_count += 1
return one_nonrepeating_count, zero_repeating_count, zero_nonrepeating_count, odd_periodic_count, even_periodic_count
# 65 Convergents of e
# The square root of 2 can be written as an infinite continued fraction.
# The infinite continued fraction can be written, v2 = [1;(2)], (2) indicates that 2 repeats ad infinitum.
# In a similar way, v23 = [4;(1,3,1,8)].
# (see page)
# Hence the sequence of the first ten convergents for v2 are:
# 1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ...
# What is most surprising is that the important mathematical constant,
# e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
# The first ten terms in the sequence of convergents for e are:
# 2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...
# The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17.
# Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.
def continued_fraction_approximation(coeff_list, n):
"""Return fractional approximations of continued fractions given a list of coefficients.
:param coeff_list: of the form [a1, a2, (b1, b2, b3, ...)] where there is at least one non-repeating coeff a1.
:param n: number of coefficients to use in the approximation
:return: (numerator, denominator, fractional approximation)
"""
if isinstance(coeff_list[-1], tuple):
nonrepeating_len = len(coeff_list) - 1
repeating_len = len(coeff_list[-1])
else:
nonrepeating_len = len(coeff_list)
repeating_len = 0
if nonrepeating_len < n:
# error !!!
pass
num = 0
den = 1
if n > 1:
if n <= nonrepeating_len:
for index in reversed(range(1, n)):
num, den = den, coeff_list[index]*den + num
else:
repeat_list = coeff_list[-1]
repeat_n = n - nonrepeating_len
# initial loop
start_index = repeat_n % repeating_len
if start_index > 0:
for index in reversed(range(start_index)):
num, den = den, repeat_list[index]*den + num
# repeated loops
for repeat_index in range(repeat_n // repeating_len):
for index in reversed(range(repeating_len)):
num, den = den, repeat_list[index]*den + num
# non-repeating coeffs
if nonrepeating_len > 1:
nonrepeat_list = coeff_list[:-1]
for index in reversed(range(1, nonrepeating_len)):
num, den = den, nonrepeat_list[index]*den + num
den, num = den, coeff_list[0]*den + num
return num, den, num/den
def generate_continued_fraction_coeffs_e(n):
"""Return the first n coefficients for continued fraction expansion of e."""
coeff_list = [2]
for index in range(1, 2+(n-1)//3):
coeff_list.extend([1, 2*index, 1])
return coeff_list[:n]
# 66 Diophantine equation
# Consider quadratic Diophantine equations of the form:
# x**2 - Dy**2 = 1
# For example, when D=13, the minimal solution in x is 649**2 - 13x180**2 = 1.
# It can be assumed that there are no solutions in positive integers when D is square.
# By finding minimal solutions in x for D = {2, 3, 5, 6, 7}, we obtain the following:
# 3**2 - 2x2**2 = 1
# 2**2 - 3x1**2 = 1
# 9**2 - 5x4**2 = 1
# 5**2 - 6x2**2 = 1
# 8**2 - 7x3**2 = 1
# Hence, by considering minimal solutions in x for D <= 7, the largest x is obtained when D=5.
# Find the value of D <= 1000 in minimal solutions of x for which the largest value of x is obtained.
def pells_equation_min_solutions(d, max_y =10 ** 6):
"""Return the minimal x,y solution to Pell's equation (x**2 - Dy**2 = 1) for a given D.
This version single increments y to search.
:param d: D variable of Pell's equation above.
:param max_y: Max y value to try
:return: Minimal solution in the form (x, y)
"""
x = int(math.sqrt(d))
y = 1
found = False
while y < max_y:
ans = x*x - d*y*y
if ans == 1:
found = True
break
elif ans > 1:
y += 1
x = int(math.sqrt(d*y*y))
else:
x += 1
if not found:
x = 0
y = 0
return x, y
def pells_equation_min_solutions2(d, max_n = 100):
"""Return the minimal x,y solution to Pell's equation (x**2 - Dy**2 = 1) for a given D.
This version uses square root continued fraction approximation to generate (x, y) combinations to try.
:param d: D variable of Pell's equation above.
:param max_n: Max number of continued fraction coefficents to try
:return: Minimal solution in the form (x, y)
"""
coeff_list = sqrt_continued_fraction_coeffs(d)
found = False
for n in range(1, max_n):
x, y, fraction = continued_fraction_approximation(coeff_list, n)
if x*x - d*y*y == 1:
found = True
break
if not found:
x = 0
y = 0
return x, y
# 67 Maximum path sum II
# 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.
# 3
# 7 4
# 2 4 6
# 8 5 9 3
# That is, 3 + 7 + 4 + 9 = 23.
# Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'),
# a 15K text file containing a triangle with one-hundred rows.
# NOTE: This is a much more difficult version of Problem 18.
# It is not possible to try every route to solve this problem, as there are 2**99 altogether!
# If you could check one trillion (10**12) routes every second it would take over twenty billion years to check them all.
# There is an efficient algorithm to solve it. ;o)
def max_sum_triangle2(tri_array):
"""Given a number triangle, return a triangle with the maximnum sum of previous rows at each node.
:param tri_array: array representing the number triangle
:return: array with the maximum sum at each node
"""
max_sum_array = [tri_array[0]] # row 0
for tri_row in tri_array[1:]:
max_sum_row = [tri_row[0] + max_sum_array[-1][0]]
for i in range(1, len(tri_row)):
max_sum_row.append(tri_row[i] + max(max_sum_array[-1][i-1:i+1]))
max_sum_array.append(max_sum_row)
return max_sum_array
# 68 Magic 5-gon ring
# Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.
# (see page)
# Working clockwise, and starting from the group of three with the numerically lowest external node
# (4,3,2 in this example), each solution can be described uniquely.
# For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.
# It is possible to complete the ring with four different totals: 9, 10, 11, and 12.
# There are eight solutions in total.
# Total Solution Set
# 9 4,2,3; 5,3,1; 6,1,2
# 9 4,3,2; 6,2,1; 5,1,3
# 10 2,3,5; 4,5,1; 6,1,3
# 10 2,5,3; 6,3,1; 4,1,5
# 11 1,4,6; 3,6,2; 5,2,4
# 11 1,6,4; 5,4,2; 3,2,6
# 12 1,5,6; 2,6,4; 3,4,5
# 12 1,6,5; 3,5,4; 2,4,6
# By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.
# Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings.
# What is the maximum 16-digit string for a "magic" 5-gon ring?
# Solution Strategy:
# 1. Find the different combinations for the five external nodes
# 2. Check the different permutations for the five internal nodes
def find_magic_ring(ring_size, external_nodes, internal_nodes):
"""Return a set of magic rings, each of which uses each external node once, and internal node once,
and have the same sum.
:param ring_size: Number of inner nodes on the ring (same as the number of outer nodes)
:param external_nodes: List of numbers to be used as external nodes.
Each number on this list will be used once for each magic ring.
:param internal_nodes: List of numbers to be used as internal nodes.
Each number on this list will be used once for each magic ring.
:return: List of magic rings having the specified external and internal nodes in the form:
[[(external1, internal1, internal2), (external2, internal2, internal3), ... ring_size number of tuples], [next magic ring]...]
"""
# Calculate the only possible magic sum for a given set of external/internal node values.
expected_magic_sum = (sum(external_nodes) + 2*sum(internal_nodes)) // ring_size
# Generate sets of nodes with the same sum
magic_sum_sets = []
for e_node in external_nodes:
for i_node1 in internal_nodes:
i_node2 = expected_magic_sum - e_node - i_node1
if i_node2 in internal_nodes and i_node2 != i_node1:
magic_sum_sets.append((e_node, i_node1, i_node2))
# Generate rings
magic_rings = []
if len(magic_sum_sets) >= ring_size:
external_node_permutations_count = common.permutations(ring_size-1, ring_size-1)
for permutation_index in range(external_node_permutations_count):
external_node_list =[external_nodes[0]]
external_node_list.extend(common.nth_permutation(permutation_index, external_nodes[1:]))
magic_rings.extend(next_magic_node([], external_node_list, magic_sum_sets))
# Delete sets that do not cycle back from last node to first node
for magic_ring in reversed(magic_rings):
if magic_ring[0][1] != magic_ring[-1][2]:
magic_rings.remove(magic_ring)
return magic_rings
def next_magic_node(input_node_list, external_node_list, magic_sum_sets):
"""Recursive function generating magic ring one node at a time.
:param input_node_list: Partially completed node list. Is empty list [] if first node.
:param external_node_list: External node values yet to be added in correct order.
Is empty list [] if magic ring is complete.
:param magic_sum_sets: Sum sets in the form (external, internal1, internal2) to be used to construct magic ring.
:return: List of magic rings found for a given external/internal nodes list,
or empty list [] if none found.
"""
#
if not external_node_list:
return [input_node_list]
elif not input_node_list:
output_node_list = []
for index in range(len(magic_sum_sets)):
temp = input_node_list[:]
if external_node_list[0] == magic_sum_sets[index][0]:
temp.append(tuple(magic_sum_sets[index]))
temp = next_magic_node(temp[:], external_node_list[1:], magic_sum_sets[:index] + magic_sum_sets[index+1:])
output_node_list.extend(temp)
return output_node_list
else:
output_node_list = []
for index in range(len(magic_sum_sets)):
temp = input_node_list[:]
if (external_node_list[0] == magic_sum_sets[index][0]) and (temp[-1][2] == magic_sum_sets[index][1]):
temp.append(tuple(magic_sum_sets[index]))
temp = next_magic_node(temp[:], external_node_list[1:], magic_sum_sets[:index] + magic_sum_sets[index+1:])
output_node_list.extend(temp)
return output_node_list
def generate_magic_rings(ring_size, number_list):
"""Return all magic rings or a given ring_size composed of number_lists.
Each value in number_list is used just once per magic ring.
:param ring_size: Number of inner nodes on the ring (same as the number of outer nodes)
:param number_list: Values to be used to construct magic ring. Each value to be used at most once per ring.
:return: List of all magic rings found.
"""
# Generate sets of indices into number_list for ring of ring_size
combinations_list = common.generate_combinations_list(len(number_list), ring_size)
# Generate sets of external nodes
# For magic ring, each set of external nodes must sum to a multiple of ring_size
external_ring_list = []
for combination in combinations_list:
external_node_list = []
for index in combination:
external_node_list.append(number_list[index])
if sum(external_node_list) % ring_size == 0:
external_ring_list.append(external_node_list)
# Generate sets of internal node combinations
# For magic ring, each set of internal nodes must sum to a multiple of ring_size
internal_ring_list = []
for external_node_list in reversed(external_ring_list):
internal_node_list = [i for i in number_list if i not in external_node_list]
if sum(internal_node_list) % ring_size == 0:
internal_ring_list.insert(0, internal_node_list)
else:
external_ring_list.remove(external_node_list)
# Find all magic rings for a given external/internal ring combination
magic_ring_list = []
for index in range(len(external_ring_list)):
magic_ring_list.extend(find_magic_ring(ring_size, external_ring_list[index], internal_ring_list[index]))
# Sort magic_ring_list
changed = True
len_list = len(magic_ring_list)
while changed:
changed = False
for index1 in range(len_list-1):
found = False
for index2 in range(ring_size):
for index3 in range(3):
if magic_ring_list[index1][index2][index3] > magic_ring_list[index1+1][index2][index3]:
magic_ring_list[index1], magic_ring_list[index1+1] = magic_ring_list[index1+1], magic_ring_list[index1]
changed = True
found = True
break
elif magic_ring_list[index1][index2][index3] < magic_ring_list[index1+1][index2][index3]:
found = True
break
if found:
break
return magic_ring_list
# 69 Totient maximum
# Euler's Totient function, f(n) [sometimes called the totient function], is used to determine the number of numbers
# less than n which are relatively prime to n.
# For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, f(9)=6.
# n Rel. Prime f(n) n/f(n)
# 2 1 1 2
# 3 1,2 2 1.5
# 4 1,3 2 2
# 5 1,2,3,4 4 1.25
# 6 1,5 2 3
# 7 1,2,3,4,5,6 6 1.1666...
# 8 1,3,5,7 4 2
# 9 1,2,4,5,7,8 6 1.5
# 10 1,3,7,9 4 2.5
# It can be seen that n=6 produces a maximum n/f(n) for n <= 10.
# Find the value of n <= 1,000,000 for which n/f(n) is a maximum.
def maximum_totient_ratio_simple(max_n):
"""Return number n < max_n with maximum n/totient(n) ratio.
Per analysis, the n with maximum n/totient(n) for any given range will be an even number.
:param max_n: maximum n to check
:return: n with the maximum n/totient(n) ratio in the form: (n, totient(n), n/totient(n))
"""
max_ratio = (0, 0, 0)
for n in range(2, max_n+1, 2): # Check only even n
f = common.totient(n)
ratio = n/f
if max_ratio[-1] < ratio:
max_ratio = (n, f, ratio)
return max_ratio
def maximum_totient_ratio_odd(max_n):
"""Return number n < max_n with maximum n/totient(n) ratio.
Per analysis, the n with maximum n/totient(n) for any given range will be an even number that is twice an odd number
because of the property:
totient(2m) = 2*totient(m) if m is even
= totient(m) if m is odd
:param max_n: maximum n to check
:return: n with the maximum n/totient(n) ratio in the form: (n, totient(n), n/totient(n))
"""
max_ratio = (0, 0, 0)
for n in range(1, 1+max_n//2, 2): # Only odd n
f = common.totient(n)
ratio_2n = 2*n/f
if max_ratio[-1] < ratio_2n:
max_ratio = (2*n, f, ratio_2n)
return max_ratio
def maximum_totient_ratio_prime(max_n):
"""Return number n < max_n with maximum n/totient(n) ratio.
Euler's product formula:
n = (p1**k1) * (p2**k2) * ...
totient(n) = n * (1 - 1/p1) * (1 - 1/p2) * ...
= n * (p1 - 1)/p1 * (p2 -1)/p2 * ...
n/totient(n) = (p1*p2*...)/((p1-1)*(p2-1)*...) -> increases as more prime are added.
where p1, p2, ... are primes.
Per analysis, the n with maximum n/totient(n) for any given range will be the product of primes since that number
has the maximum count of distinct primes p1, p2, ...
:param max_n: maximum n to check
:return: n with the maximum n/totient(n) ratio in the form: (n, totient(n), n/totient(n))
"""
prime_list = common.prime_list_mr(0, 1000)
n = 1
ratio = 1.0
for prime in prime_list:
n *= prime
ratio *= prime/(prime-1)
if n > max_n:
n = n // prime
ratio *= (prime-1)/prime
break
f = int(0.5 + n/ratio)
max_ratio = (n, f, ratio)
return max_ratio
# Problem 60-69 Checks
if __name__ == '__main__': # only if run as a script, skip when imported as module