-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolutions2.py
159 lines (128 loc) · 3.4 KB
/
solutions2.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
import ipdb
import solutions
def get_amicable_pair(num):
a = list(solutions.factors(num))
a.remove(num)
a = sum(a)
b = list(solutions.factors(a))
b.remove(a)
b = sum(b)
if b == num and a != b:
return a, b
return None
def sum_of_all_amicable_numbers(limit):
"""
>>> sum_of_all_amicable_numbers(10000)
31626
"""
items = []
for num in range(1, limit):
if num in items or solutions.is_prime(num):
continue
result = get_amicable_pair(num)
if result:
a, b = result
if a not in items:
items.append(a)
if b not in items:
items.append(b)
return sum(items)
def lenth_of_fib(length):
index = 1
while True:
val = solutions.fib(index)
if len(str(val)) == length:
return val
index += 1
def permute(xs, low=0):
if low + 1 >= len(xs):
yield xs
else:
for p in permute(xs, low + 1):
yield p
for i in range(low + 1, len(xs)):
xs[low], xs[i] = xs[i], xs[low]
for p in permute(xs, low + 1):
yield p
xs[low], xs[i] = xs[i], xs[low]
def list_to_num(items):
return "".join([str(i) for i in items])
def nth_permutation(series, nth):
#permute_obj = permute(series, len(series))
permute_obj = permute(series)
index = 1
#fl_obj = open('/tmp/combinations.txt', 'w')
items = []
while True:
value = permute_obj.next()
#fl_obj.write('%s\n'%value)
items.append(list_to_num(value))
if index == nth:
break
index += 1
return items
#fl_obj.close()
def first_fib_limit(limit):
"""
>>> first_fib_limit(100)
476
>>> first_fib_limit(3)
12
>>> first_fib_limit(1)
1
"""
cached = {}
index = 1
while True:
val = solutions.fib(index, cached)
if len(str(val)) == limit:
return index
cached[index] = val
index += 1
def get_recurring_number(val):
val = str(val).split('.')[1]
num_str = ''
for c in val:
if c in num_str:
return num_str
num_str += c
def longest_recurring_decimal_fraction(limit):
for index in range(2, limit):
val = 1/float(index)
print get_recurring_number(val)
def is_abundant_number(num):
factors = list(solutions.factors(num))
factors.remove(num)
return sum(factors) > num
def abundant_generator(seed=None):
i = seed or 12
while True:
if is_abundant_number(i):
yield i
i += 1
def is_sum_of_2abundant(num):
if num < 24:
return False
elif num == 24:
return True
abundant_gen = abundant_generator()
val = abundant_gen.next()
smallest_abundant = 12
while True:
remaining = num - val
if remaining == val or remaining == smallest_abundant:
return True
elif remaining < smallest_abundant:
return False
elif is_abundant_number(remaining):
return True
val = abundant_gen.next()
def non_abundant_sum(limit=28123):
"""
>>> non_abundant_sum()
4179871
"""
return sum([i for i in xrange(limit+1) if not is_sum_of_2abundant(i)])
if __name__ == '__main__':
import doctest
doctest.testmod()