-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.py
More file actions
135 lines (125 loc) · 2.17 KB
/
euler.py
File metadata and controls
135 lines (125 loc) · 2.17 KB
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
import math
import collections
from numba import jit
'''
64 bit prime: 18446744073709551557
'''
'''
Inputs:
n: An integer to generate primes up to and including n.
Outputs:
array: An array of primes.
Example:
n = 10, returns [2,3,5,7]
n = 11, returns [2,3,5,7,11]
'''
@jit
def sieve_of_eratosthenes(n):
array = [0]+[0]+[1]*(n-1)
out = []
for i in range(2,int(math.ceil(math.sqrt(n)))):
if array[i]==1:
for j in range(i*i,n+1,i):
array[j] = 0
for i in range(len(array)):
if array[i]==1:
out.append(i)
return out
'''
Inputs:
n: An integer for prime decomposition.
Outputs:
array: An array of prime factors, with repeats.
Example:
n = 1000, returns [2,2,2,5,5,5]
n = 10, returns [2,5]
'''
@jit
def prime_decomposition(n,sieve):
array = []
for i in sieve:
while True:
if n%i==0:
n/=i
array.append(i)
else:
break
if n==1:
break
return array
'''
Inputs:
Outputs:
Example:
'''
def modular_pow(base, exponent, modulus):
if (modulus-1)**2 > (2**64-1): raise Exception( "Invalid Modulus. Use a smaller one." )
if modulus == 1:
return 0
result = 1
base = base % modulus
while exponent > 0:
if (exponent%2)==1:
result = (result * base) % modulus
quotient = (result * base) // modulus
print quotient
exponent = exponent >> 1
base = (base**2) % modulus
return result
'''
Inputs:
l: A list of numbers.
Outputs:
out: All numbers in the list multiplied together.
Example:
l = [2,3,4,5,6], returns 720
'''
@jit
def multiply_list(l):
out = 1
for element in l:
out *= element
return out
@jit
def list_to_dic(l):
dic = {}
for i in l:
dic[i] = -1
return dic
@jit
def factorial(n):
if n==0:
return 1
else:
ans = 1
for i in range(1,n+1):
ans *= i
return ans
@jit
def is_square(n):
sq = math.sqrt(n)
if (int(sq))**2 == n:
return True
return False
'''
Inputs:
str: A string.
Outputs:
True or False.
Example:
str = "585", returns True
str = "589", returns False
'''
@jit
def check_palindrome(str):
for i in range(len(str)):
if str[i] != str[-1-i]:
return False
return True
def count_unique_digits(n):
dic = collections.defaultdict(int)
while (n / 10) != 0:
dic[n % 10] += 1
n /= 10
dic[n % 10] += 1
return dic