first commit

This commit is contained in:
=
2024-02-07 15:06:50 +01:00
commit af02e453c1
55 changed files with 2166 additions and 0 deletions

1
launch.py Normal file
View File

@@ -0,0 +1 @@
import p12.solution

Binary file not shown.

7
p10/solution.py Normal file
View File

@@ -0,0 +1,7 @@
from util.sieve import sieve
def solution(n):
p, s = sieve(n)
return sum(p)
print(solution(2000000))

26
p102/solution.jl Normal file
View File

@@ -0,0 +1,26 @@
function solution(file)
triangles_raw = read(file, String)
triangles = split(triangles_raw, "\n")
c = 0
for triangle_raw triangles
triangle = split(strip(triangle_raw), ",")
∠A = atan(parse(Int, triangle[2]), parse(Int, triangle[1]))
∠B = atan(parse(Int, triangle[4]), parse(Int, triangle[3]))
∠C = atan(parse(Int, triangle[6]), parse(Int, triangle[5]))
∠AB = (∠A, ∠B)
∠AC = (∠A, ∠C)
if (∠AB*∠AC) >= 0 continue end
∠BA = (∠B, ∠A)
∠BC = (∠B, ∠C)
if (∠BA*∠BC) < 0 c += 1 end
end
return c
end
function (∠A, ∠B)
∠AB = mod2pi(∠B - ∠A + π) - π
return ∠AB
end

1000
p102/triangles.txt Normal file

File diff suppressed because it is too large Load Diff

15
p104/solution.jl Normal file
View File

@@ -0,0 +1,15 @@
function solution()
fn = fp = n = BigInt(1)
while true
fn, fp, n = fn+fp, fn, n+1
if is_pandigital(fn) return n+1 end
end
end
function is_pandigital(n)
if log10(n) + 1 < 9 return false end
d = digits(n)
S = Set(1:9)
return Set(d[1:9]) == S && Set(d[end-8:end]) == S
end

Binary file not shown.

65
p11/solution.py Normal file
View File

@@ -0,0 +1,65 @@
def solution(matrix, n):
x = len(matrix[0])
y = len(matrix)
max_product = 0
# -
for i in range(y):
for j in range(x-n+1):
product = 1
for k in range(n):
product *= matrix[i][j+k]
max_product = max(max_product, product)
# |
for i in range(y-n+1):
for j in range(x):
product = 1
for k in range(n):
product *= matrix[i+k][j]
max_product = max(max_product, product)
# /
for i in range(x-n+1):
for j in range(y-n+1):
product = 1
for k in range(n):
product *= matrix[i+n-1-k][j+k]
max_product = max(max_product, product)
# \
for i in range(x-n+1):
for j in range(y-n+1):
product = 1
for k in range(n):
product *= matrix[i+k][j+k]
max_product = max(max_product, product)
return max_product
matrix = [
[ 8, 2, 22, 97, 38, 15, 00, 40, 00, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 00],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
[21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 00, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
[ 4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[ 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
]
print(solution(matrix, 4))

Binary file not shown.

21
p12/solution.py Normal file
View File

@@ -0,0 +1,21 @@
from util.sieve import extended_sieve as sieve
from util.prod import prod
def solution(n):
i = 1
max_factors = 1
s = sieve()
while True:
i += 1
p_dec_0 = s.get(i)[1]
p_dec_1 = s.get(i+1)[1]
n_factors_0 = prod([p_dec_0[key]+1 if key != 2 else p_dec_0[key] for key in p_dec_0])
n_factors_1 = prod([p_dec_1[key]+1 if key != 2 else p_dec_1[key] for key in p_dec_1])
n_factors_product = n_factors_0 * n_factors_1
if n_factors_product > max_factors:
max_factors = n_factors_product
print(max_factors)
if n_factors_product > n:
return i*(i+1)/2, n_factors_product
print(solution(500))

111
p13/solution.py Normal file
View File

@@ -0,0 +1,111 @@
from math import log10, floor
def solution(numbers, n):
numbers_sum = sum(numbers)
n_digits = floor(log10(numbers_sum)) + 1
return numbers_sum//(10**(n_digits-n))
numbers = [
37107287533902102798797998220837590246510135740250,
46376937677490009712648124896970078050417018260538,
74324986199524741059474233309513058123726617309629,
91942213363574161572522430563301811072406154908250,
23067588207539346171171980310421047513778063246676,
89261670696623633820136378418383684178734361726757,
28112879812849979408065481931592621691275889832738,
44274228917432520321923589422876796487670272189318,
47451445736001306439091167216856844588711603153276,
70386486105843025439939619828917593665686757934951,
62176457141856560629502157223196586755079324193331,
64906352462741904929101432445813822663347944758178,
92575867718337217661963751590579239728245598838407,
58203565325359399008402633568948830189458628227828,
80181199384826282014278194139940567587151170094390,
35398664372827112653829987240784473053190104293586,
86515506006295864861532075273371959191420517255829,
71693888707715466499115593487603532921714970056938,
54370070576826684624621495650076471787294438377604,
53282654108756828443191190634694037855217779295145,
36123272525000296071075082563815656710885258350721,
45876576172410976447339110607218265236877223636045,
17423706905851860660448207621209813287860733969412,
81142660418086830619328460811191061556940512689692,
51934325451728388641918047049293215058642563049483,
62467221648435076201727918039944693004732956340691,
15732444386908125794514089057706229429197107928209,
55037687525678773091862540744969844508330393682126,
18336384825330154686196124348767681297534375946515,
80386287592878490201521685554828717201219257766954,
78182833757993103614740356856449095527097864797581,
16726320100436897842553539920931837441497806860984,
48403098129077791799088218795327364475675590848030,
87086987551392711854517078544161852424320693150332,
59959406895756536782107074926966537676326235447210,
69793950679652694742597709739166693763042633987085,
41052684708299085211399427365734116182760315001271,
65378607361501080857009149939512557028198746004375,
35829035317434717326932123578154982629742552737307,
94953759765105305946966067683156574377167401875275,
88902802571733229619176668713819931811048770190271,
25267680276078003013678680992525463401061632866526,
36270218540497705585629946580636237993140746255962,
24074486908231174977792365466257246923322810917141,
91430288197103288597806669760892938638285025333403,
34413065578016127815921815005561868836468420090470,
23053081172816430487623791969842487255036638784583,
11487696932154902810424020138335124462181441773470,
63783299490636259666498587618221225225512486764533,
67720186971698544312419572409913959008952310058822,
95548255300263520781532296796249481641953868218774,
76085327132285723110424803456124867697064507995236,
37774242535411291684276865538926205024910326572967,
23701913275725675285653248258265463092207058596522,
29798860272258331913126375147341994889534765745501,
18495701454879288984856827726077713721403798879715,
38298203783031473527721580348144513491373226651381,
34829543829199918180278916522431027392251122869539,
40957953066405232632538044100059654939159879593635,
29746152185502371307642255121183693803580388584903,
41698116222072977186158236678424689157993532961922,
62467957194401269043877107275048102390895523597457,
23189706772547915061505504953922979530901129967519,
86188088225875314529584099251203829009407770775672,
11306739708304724483816533873502340845647058077308,
82959174767140363198008187129011875491310547126581,
97623331044818386269515456334926366572897563400500,
42846280183517070527831839425882145521227251250327,
55121603546981200581762165212827652751691296897789,
32238195734329339946437501907836945765883352399886,
75506164965184775180738168837861091527357929701337,
62177842752192623401942399639168044983993173312731,
32924185707147349566916674687634660915035914677504,
99518671430235219628894890102423325116913619626622,
73267460800591547471830798392868535206946944540724,
76841822524674417161514036427982273348055556214818,
97142617910342598647204516893989422179826088076852,
87783646182799346313767754307809363333018982642090,
10848802521674670883215120185883543223812876952786,
71329612474782464538636993009049310363619763878039,
62184073572399794223406235393808339651327408011116,
66627891981488087797941876876144230030984490851411,
60661826293682836764744779239180335110989069790714,
85786944089552990653640447425576083659976645795096,
66024396409905389607120198219976047599490197230297,
64913982680032973156037120041377903785566085089252,
16730939319872750275468906903707539413042652315011,
94809377245048795150954100921645863754710598436791,
78639167021187492431995700641917969777599028300699,
15368713711936614952811305876380278410754449733078,
40789923115535562561142322423255033685442488917353,
44889911501440648020369068063960672322193204149535,
41503128880339536053299340368006977710650566631954,
81234880673210146739058568557934581403627822703280,
82616570773948327592232845941706525094512325230608,
22918802058777319719839450180888072429661980811197,
77158542502016545090413245809786882778948721859617,
72107838435069186155435662884062257473692284509516,
20849603980134001723930671666823555245252804609722,
53503534226472524250874054075591789781264330331690
]
print(solution(numbers, 10))

34
p14/solution.py Normal file
View File

@@ -0,0 +1,34 @@
def solution(n):
n_steps = {}
i = 1
number = 1
chain = [1]
result = (i, len(chain))
while i < n:
if number == 1:
chain_length = len(chain)
for j in range(chain_length-1):
n_steps[chain[j]] = chain_length - j
if chain_length > result[1]:
result = (i, chain_length)
i += 1
number = i
chain = [i]
if number in n_steps:
chain_length = len(chain) + n_steps[number] - 1
for j in range(len(chain)-1):
n_steps[chain[j]] = chain_length - j
if chain_length > result[1]:
result = (i, chain_length)
i += 1
number = i
chain = [i]
elif number % 2 == 0:
number //= 2
chain.append(number)
else:
number = 3*number +1
chain.append(number)
return result
print(solution(1000000))

6
p15/solution.py Normal file
View File

@@ -0,0 +1,6 @@
from math import factorial
def solution(a, b):
return factorial(a+b) / (factorial(a) * factorial(b))
print(solution(20, 20))

4
p16/solution.py Normal file
View File

@@ -0,0 +1,4 @@
def solution(n):
return sum([int(d) for d in str(2**n)])
print(solution(1000))

29
p17/solution.py Normal file
View File

@@ -0,0 +1,29 @@
def solution():
# 1 digit
l1 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
d1 = len(combine_words(l1))
# 2 digits
l2 = ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
l3 = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
d2 = len(combine_words(l2)) + d1*len(l2) + len(combine_words(l2))*(len(l1)+1)
# 3 digits
w1 = 'hundred'
w2 = 'and'
d3 = len(w1)*900 + d1*100 + len(w2)*9*99 + 9*d2
# 4 digits
w3 = 'thousand'
d4 = len(w3)
return d1 + d2 + d3 + d4
def combine_words(words):
w = ''
for word in words:
w += word
return w
# solved by chatGPT
print(solution())

41
p18/solution.jl Normal file
View File

@@ -0,0 +1,41 @@
function solution(T)
a, b = size(T);
if a == b
n = a
else
return
end
weights = zeros(n, n)
for i = 1:n
i = n-i+1
println(i)
for j = 1:i
if i == n
weights[i, j] = T[i, j]
else
weights[i, j] = max(weights[i+1, j], weights[i+1, j+1]) + T[i, j]
end
end
end
return weights[1, 1]
end
T = [
75 0 0 0 0 0 0 0 0 0 0 0 0 0 0
95 64 0 0 0 0 0 0 0 0 0 0 0 0 0
17 47 82 0 0 0 0 0 0 0 0 0 0 0 0
18 35 87 10 0 0 0 0 0 0 0 0 0 0 0
20 04 82 47 65 0 0 0 0 0 0 0 0 0 0
19 01 23 75 03 34 0 0 0 0 0 0 0 0 0
88 02 77 73 07 63 67 0 0 0 0 0 0 0 0
99 65 04 28 06 16 70 92 0 0 0 0 0 0 0
41 41 26 56 83 40 80 70 33 0 0 0 0 0 0
41 48 72 33 47 32 37 16 94 29 0 0 0 0 0
53 71 44 65 25 43 91 52 97 51 14 0 0 0 0
70 11 33 28 77 73 17 78 39 68 17 57 0 0 0
91 71 52 38 17 14 91 43 58 50 27 29 48 0 0
63 66 04 68 89 53 67 30 73 16 69 87 40 31 0
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
]
print(solution(T))

29
p19/solution.jl Normal file
View File

@@ -0,0 +1,29 @@
function solution()
n_months = 12
n_years = 100
n = 0
day = 1
for i = 1:n_months*n_years
if (day-1)%7+1 == 7
n += 1
end
day += step(i)
end
return n
end
function step(next::Int)
year = (next-1)÷12+1
month = (next-1)%12+1
if month [4, 6, 9, 11]
return 30
elseif month [1, 3, 5, 7, 8, 10, 12]
return 31
elseif year % 4 == 0
return 29
else return 28
end
end
# solved by chatGPT
println(solution())

17
p20/solution.jl Normal file
View File

@@ -0,0 +1,17 @@
function solution(n)
fac = factorial(big(n))
places = log10(fac)
sum = 0
for i = 0:places
clip = fac÷(10^i)
sum += clip%10
end
v = collect(string(fac))
sum2 = 0
for e v
sum2 += parse(Int, e)
end
return sum, sum2
end
print(solution(100))

48
p21/solution.jl Normal file
View File

@@ -0,0 +1,48 @@
include("../util/sieve.jl")
import .erastothenes_sieve: get_extended_sieve, run, get
function solution(n)
s = get_extended_sieve()
run(s, n)
sum = 0
for i = 1:n
spds = compute_divisors(i, s)
if spds > i
spds_2 = compute_divisors(spds, s)
if spds_2 == i
sum += spds + spds_2
println("found pair ", spds, ", ", spds_2)
end
end
end
return sum
end
function compute_divisors(value, s)
if value == 1
return 1
end
pvc = get(s, value, false)
n = sum(collect(values(pvc[2])))
pds = zeros(n)
j = 1
for key in keys(pvc[2])
for i = 1:pvc[2][key]
pds[j] = key
j += 1
end
end
divisors = Set([])
for i = 1:2^n-1
prod = 1
for j = 0:length(pds)-1
if i & (1 << j) > 0
prod *= pds[j+1]
end
end
push!(divisors, prod)
end
return round(Int, sum(collect(divisors)) + 1 - value)
end
println(solution(10000))

1
p22/p022_names.txt Normal file

File diff suppressed because one or more lines are too long

15
p22/solution.jl Normal file
View File

@@ -0,0 +1,15 @@
function solution()
file_str = ""
open(f -> file_str = read(f, String), "p22_/p022_names.txt")
file_str_cleaned = replace(file_str, "\"" => "")
names = sort!(split(file_str_cleaned, ","))
n = length(names)
f = char -> (Int(char) - 1)%32 + 1
score = 0
for i = 1:n
score += sum(f.(collect(names[i])))*i
end
return score
end
println(solution())

46
p23/main.jl Normal file
View File

@@ -0,0 +1,46 @@
include("./../util/julia/erastothenes_extended_sieve.jl")
import .erastothenes_extended_sieve: extended_sieve, get_extended_sieve, run!
import Combinatorics: multiset_combinations
function answer(max)
b = BitVector(undef, max)
a = Vector{Int}(undef, 0)
s = get_extended_sieve()
run!(s, max)
for i 1:max
if ~s.sieve.sieve[i] && is_abundant(s.factors[i])
push!(a, i)
end
end
for i a
for j a
if i+j <= max
b[i+j] = true
end
end
end
sum = 0
for i 1:max
if ~b[i]
sum += i
end
end
return sum
end
function is_abundant(V)
n = length(V)
sum = 1
for i 1:n-1
for j multiset_combinations(V, i)
sum += prod(j)
end
end
return sum > prod(V)
end

32
p24/solution.jl Normal file
View File

@@ -0,0 +1,32 @@
function solution(V, p)
l = length(V)
n = factorial(l)
if p > n
print("p too large")
return
end
sort!(V)
m = n
r = p
vals = zeros(Int, l-1)
for i 0:(l-2)
m = div(m, l-i)
vals[i+1] = cld(r, m)
r = mod(r-1, m)+1
end
sol = zeros(Int, l)
V = copy(V)
for i 1:(l-1)
sol[i] = popat!(V, vals[i])
end
sol[l] = pop!(V)
return sol
end
function printsols(V)
for i 1:factorial(length(V))
println(solution(V, i))
end
end

11
p25/solution.jl Normal file
View File

@@ -0,0 +1,11 @@
function solution(n_digits)
n = 1
f_n = BigInt(1)
f_n_1 = 0
while log10(f_n)+1 < n_digits
f_n_1, f_n = f_n, f_n + f_n_1
n += 1
end
return n
end

38
p26_/solution.jl Normal file
View File

@@ -0,0 +1,38 @@
function solution(n_max)
longest_cycle = 0
cycle_length = 0
for i 2:n_max
if check_if_finite(i)
continue
end
println(i)
n = 1
p = i
while ~(Set(digits(p)) == Set(9))
p *= i
@info n += 1
end
if n > cycle_length
longest_cycle = i
cycle_length = n
end
end
return longest_cycle, cycle_length
end
function check_if_finite(n)
print("checking ")
println(n)
k = n
while k%2 == 0
k = div(k, 2)
end
while k%5 == 0
k = div(k, 5)
end
return k == 1
end

37
p27/solution.jl Normal file
View File

@@ -0,0 +1,37 @@
include("../util/sieve.jl")
import .erastothenes_sieve: get_sieve, is_prime, get_primes, run
function solution()
s = get_sieve()
run(s, 1000)
amax = 0
bmax = 0
nmax = 0
for a = -999:2:999
println("value for a: ", a)
for b = get_primes(s, 1000)
if a+b > 1
n = 0
number = n*n+a*n+b
while is_prime(s, number)
n += 1
number = n*n+a*n+b
end
if n > nmax
amax = a
bmax = b
nmax = n
println("found new maximum for:")
println("a= ", a)
println("b= ", b)
println("n= ", n)
end
end
end
end
println('\n')
println("a: ", amax, " b: ", bmax, " n: ", nmax)
println(amax*bmax)
end
solution()

12
p28/solution.jl Normal file
View File

@@ -0,0 +1,12 @@
function solution(n)
if n % 2 == 0
return -1
end
if n == 1
return 1
else
sum_n = 4*n^2 - 6*(n-1)
return solution(n-2) + sum_n
end
end

10
p29/solution.jl Normal file
View File

@@ -0,0 +1,10 @@
function solution(a, b)
S = Set{BigInt}()
for i 2:a
for j 2:b
push!(S, BigInt(i)^j)
end
end
return length(S)
end

11
p30/solution.jl Normal file
View File

@@ -0,0 +1,11 @@
function solution(p)
V = Vector{Int}(undef, 0)
for i 2:999999
if i == sum(digits(i).^p)
println(i)
push!(V, i)
end
end
return V
end

11
p314_/solution.py Normal file
View File

@@ -0,0 +1,11 @@
import numpy as np
import math
n = 250
R = np.add(list(range(np.ceil(n*1.41))), 1)
LL = np.subtract(np.square(R), n**2)
L = np.clip(LL, 0, LL)
A_tri = np.divide(np.add(L, n), 2)
A_seg = np.multiply(np.divide(np.arccos(np.divide(n, L)), ), np.multiply(np.square(R), math.pi))

4
p31_/solution.jl Normal file
View File

@@ -0,0 +1,4 @@
function solution(limit)
end

34
p345/solution.py Normal file
View File

@@ -0,0 +1,34 @@
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import min_weight_full_bipartite_matching
A = np.array([
[ 7, 53, 183, 439, 863, 497, 383, 563, 79, 973, 287, 63, 343, 169, 583],
[627, 343, 773, 959, 943, 767, 473, 103, 699, 303, 957, 703, 583, 639, 913],
[447, 283, 463, 29, 23, 487, 463, 993, 119, 883, 327, 493, 423, 159, 743],
[217, 623, 3, 399, 853, 407, 103, 983, 89, 463, 290, 516, 212, 462, 350],
[960, 376, 682, 962, 300, 780, 486, 502, 912, 800, 250, 346, 172, 812, 350],
[870, 456, 192, 162, 593, 473, 915, 45, 989, 873, 823, 965, 425, 329, 803],
[973, 965, 905, 919, 133, 673, 665, 235, 509, 613, 673, 815, 165, 992, 326],
[322, 148, 972, 962, 286, 255, 941, 541, 265, 323, 925, 281, 601, 95, 973],
[445, 721, 11, 525, 473, 65, 511, 164, 138, 672, 18, 428, 154, 448, 848],
[414, 456, 310, 312, 798, 104, 566, 520, 302, 248, 694, 976, 430, 392, 198],
[184, 829, 373, 181, 631, 101, 969, 613, 840, 740, 778, 458, 284, 760, 390],
[821, 461, 843, 513, 17, 901, 711, 993, 293, 157, 274, 94, 192, 156, 574],
[ 34, 124, 4, 878, 450, 476, 712, 914, 838, 669, 875, 299, 823, 329, 699],
[815, 559, 813, 459, 522, 788, 168, 586, 966, 232, 308, 833, 251, 631, 107],
[813, 883, 451, 509, 615, 77, 281, 613, 459, 205, 380, 274, 302, 35, 805]
])
A_sparse = csr_matrix(A)
I1, I2 = min_weight_full_bipartite_matching(-A_sparse)
print(I1)
print(I2)
maxima = np.zeros((1, len(I1))).squeeze()
for i in I1:
maxima[i] = A[i, I2[i]]
print(maxima)
print(sum(maxima))

24
p4/solution.py Normal file
View File

@@ -0,0 +1,24 @@
from math import log10, floor
from numpy import number
def solution(digits):
limit = 10**digits
max_palindrome = 1
numbers = (1, 1)
for i in range(2, limit):
for j in range(i, limit):
product = i*j
n_digits = floor(log10(product))+1
digits = [(product//(10**n))%10 for n in range(n_digits)]
palindrome = True
for k in range(n_digits//2):
if digits[k] != digits[-1-k]:
palindrome = False
break
if palindrome & (product > max_palindrome):
max_palindrome = product
numbers = (i, j)
return max_palindrome, numbers
print(solution(3))

30
p453_/test.py Normal file
View File

@@ -0,0 +1,30 @@
import numpy as np
def calc_quads(a, b):
for i in range(a):
for j in range(b):
pos_1 = (i, j)
for ii in range(a):
for jj in range(b):
if (ii, jj) == pos_1:
continue
pos_2 = (ii, jj)
angle_12 = np.
blocked = [pos_1, pos_2]
delta_i = ii-i
delta_j = jj-j
gcd = np.gcd(abs(delta_i), abs(delta_j))
for k in range(gcd-1):
blocked.append(pos_1+(delta_i, delta_j)*(k+1))
for iii in range(a, b):
for jjj in range(a, b):
if (iii, jjj) in blocked:
continue
pos_3 = (iii, jjj)
for iiii in range(a):
for jjjj in range(b):
if (iiii, jjjj) in blocked or (iiii, jjjj) == pos_3:
continue

26
p46/solution.jl Normal file
View File

@@ -0,0 +1,26 @@
include("../util/sieve.jl")
import .erastothenes_sieve: get_sieve, run, get, get_primes, get_composites
function solution(n::Int)
sieve = get_sieve()
run(sieve, n)
primes = get_primes(sieve)
composites = vcat([0], get_composites(sieve));
n_max = primes[end]
composite_doable = falses(n_max)
for i = 0:floor(sqrt(n))
for p = primes
number = p + 2*i*i
if number <= n_max
composite_doable[Int(number)] = true
end
end
end
for i = 1:length(composite_doable)
if !composite_doable[i] && !iseven(i)
println(i)
end
end
end
solution(10000)

13
p5/solution.py Normal file
View File

@@ -0,0 +1,13 @@
from math import gcd
def solution(n):
solution = 1
for i in range(n):
solution = lcm(solution, i+1)
return solution
def lcm(a, b):
return a*b // gcd(a, b)
print(solution(20))

14
p6/solution.py Normal file
View File

@@ -0,0 +1,14 @@
from re import S
def solution(n):
s = n*(n+1)/2
qos = s*s
soq = 0
for i in range(n):
soq += (i+1)*(i+1)
return abs(soq - qos)
print(solution(100))

Binary file not shown.

13
p7/solution.py Normal file
View File

@@ -0,0 +1,13 @@
from ctypes import util
from math import floor, sqrt
from util.sieve import sieve, extend_sieve
def solution(n):
sieve_max = n
p, s = sieve(sieve_max)
while len(p) < n:
sieve_max += n
p, s = extend_sieve(sieve_max, s)
return p[n-1]
print(solution(10001))

19
p8/solution.py Normal file
View File

@@ -0,0 +1,19 @@
from math import log10, floor
def solution(number, n):
digits = floor(log10(number)+1)
max_prod = 0
i_max = 0
for i in range(digits-n+1):
product = 1
for j in range(n):
product *= number//(10**(i+j))%10
if product > max_prod:
max_prod = product
i_max = i
return max_prod, i_max
number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
print(solution(number, 13))

8
p851_/solution.jl Normal file
View File

@@ -0,0 +1,8 @@
function solution(n)
end
function get_set_iterator(n)
end

0
p9/__init__.py Normal file
View File

13
p9/solution.py Normal file
View File

@@ -0,0 +1,13 @@
from math import gcd, sqrt, floor
def solution(s):
m = floor(sqrt(s/2))
while m > 0:
diff = s - 2*m*m
if gcd(diff, 2*m) == 2*m:
n = diff//(2*m)
if m > n & n > 0:
return(m*m-n*n, 2*m*n, m*m+n*n, (m*m-n*n)*(2*m*n)*(m*m+n*n))
m -= 1
print(solution(1000))

View File

@@ -0,0 +1,7 @@
module erastothenes_extended_sieve
include("files/_erastothenes_extended_sieve.jl")
export extended_sieve, get_extended_sieve
export run!
export get, get_primes
end

View File

@@ -0,0 +1,8 @@
module erastothenes_sieve
include("files/_erastothenes_sieve.jl")
export sieve, get_sieve
export run!
export get, get_primes, get_composites
export is_prime
end

View File

@@ -0,0 +1,59 @@
include("_erastothenes_sieve.jl")
include("_math.jl")
mutable struct extended_sieve
sieve::sieve
factors::Vector{Vector{Int}}
end
function get_extended_sieve()
return extended_sieve(sieve(falses(1), 1, 1, 1000), [[]])
end
function _elongate!(es::extended_sieve)
s = es.sieve
length = s._length
target = s._target
diff = target - length
run!(s, target)
factors = append!(es.factors, fill([], nneg(diff)))
done = [trues(length); falses(diff)]
if diff > 0
for i get_primes(s)
if i > length
factors[i] = [i]
end
for j cld(length+1, i):fld(target, i)
prod = i*j
if ~done[prod]
if done[j]
index = searchsortedfirst(factors[j], i)
factors[prod] = insert!(copy(factors[j]), index, i)
done[prod] = true
else
push!(factors[prod], i)
end
end
end
end
end
return Nothing
end
function run!(es::extended_sieve, n::Int)
es.sieve._target = n;
_elongate!(es)
return Nothing
end
function get(es::extended_sieve, n::Int, prerun=true::Bool)
if n > es.sieve._length
run(s, prerun ? n + es.sieve._prerun_size : n)
end
return (es.sieve[n], es.factors[n])
end
function get_primes(es::extended_sieve)
return get_primes(es.sieve)
end

View File

@@ -0,0 +1,88 @@
mutable struct sieve
sieve::BitVector
_length::Int
_target::Int
_prerun_size::Int
end
function get_sieve()
return sieve([false], 1, 1, 1000)
end
function _elongate!(s::sieve)
length = s._length
target = s._target
diff = target - length
if diff > 0
s.sieve = [s.sieve; trues(diff)]
for i = 1:floor(Int, sqrt(target))
if s.sieve[i]
_floor = length÷i + 1
_ceil = target÷i
for j = _floor:_ceil
actual = i*j
if actual > i
s.sieve[actual] = false
end
end
end
end
s._length = target
end
return nothing
end
# runs the sieve until index n
function run!(s::sieve, n::Int)
s._target = n
_elongate!(s)
return nothing
end
# fetches data about a specific integer
function get(s::sieve, n::Int, prerun=true::Bool)
if n > s._length
run(s, prerun ? n + s._prerun_size : n)
end
return s.sieve[n]
end
# returns all primes in the sieve
function get_primes(s::sieve, n_max=(-1)::Int)
if n_max == -1
trimmed_sieve = s.sieve
else
trimmed_sieve = s.sieve[1:n_max]
end
n = length(trimmed_sieve)
c = count(trimmed_sieve)
primes = Vector(undef, c)
j = 1
for i = 1:n
if s.sieve[i]
primes[j] = i
j += 1
end
end
return primes
end
function get_composites(s::sieve)
n = length(s.sieve)
c = count(s.sieve)
composites = Vector(undef, n-c)
j = 1
for i = 1:n
if !s.sieve[i]
composites[j] = i
j += 1
end
end
return composites
end
function is_prime(s::sieve, i::Int)
if i < 0 return false end
run(s, i)
return s.sieve[i]
end

View File

@@ -0,0 +1,3 @@
nneg(v) = max(v, 0)
npos(v) = min(v, 0)

View File

@@ -0,0 +1,31 @@
function mergeSorted(A::Vector{T}, B::Vector{T}) where T
la = length(A)
lb = length(B)
C = Vector{T}(undef, la + lb)
a = b = 1
while (a <= la && b <= lb)
if A[a] < B[b]
C[a+b-1] = A[a]
a += 1
else
C[a+b-1] = B[b]
b += 1
end
end
if a > la
while b <= lb
C[a+b-1] = B[b]
b += 1
end
else
while a <= la
C[a+b-1] = A[a]
a += 1
end
end
return C
end

6
util/julia/math.jl Normal file
View File

@@ -0,0 +1,6 @@
module math
include("files/_math.jl")
export nneg, npos
end

5
util/julia/sorting.jl Normal file
View File

@@ -0,0 +1,5 @@
module sorting
include("files/_sorting.jl")
export mergeSorted
end

Binary file not shown.

Binary file not shown.

5
util/python/prod.py Normal file
View File

@@ -0,0 +1,5 @@
def prod(l):
product = 1
for e in l:
product *= e
return product

78
util/python/sieve.py Normal file
View File

@@ -0,0 +1,78 @@
from math import sqrt, floor
class sieve:
def __init__(self):
self.sieve = [False]
self._length = 1
self._target = 1
def run(self, n):
self._target = n
self._elongate()
def get(self, n):
if n > self._length:
self.run(n)
return self.sieve[n-1]
def _elongate(self):
length = self._length
target = self._target
diff = target - length
if diff > 0:
self.sieve += [True for _ in range(diff)]
for i in range(floor(sqrt(target))):
if self.sieve[i]:
_floor = length//(i+1)
_ceil = target//(i+1)
for j in range(_floor, _ceil):
actual = (i+1)*(j+1)-1
if actual > i+1:
self.sieve[actual] = False
self._length = target
class extended_sieve:
def __init__(self):
self.sieve = [[False, 1, []]]
self._length = 1
self._target = 1
self.prerun_step = 1000
def run(self, n):
self._target = n
self._elongate()
def get(self, n):
if n > self._length:
self.run(n+self.prerun_step)
prime_decomp = {}
for e in self.sieve[n-1][2]:
if e in list(prime_decomp.keys()):
prime_decomp[e] += 1
else:
prime_decomp[e] = 1
return [self.sieve[n-1][0], prime_decomp]
def _elongate(self):
length = self._length
target = self._target
diff = target - length
if diff > 0:
self.sieve += [[True, length+i+1, []] for i in range(diff)]
for i in range(target):
if self.sieve[i][0]:
if i+1 > length:
self.sieve[i][2] += [i+1]
_floor = length//(i+1)
_ceil = target//(i+1)
for j in range(_floor, _ceil):
actual = (i+1)*(j+1)-1
if actual > i+1:
old_entry = self.sieve[actual]
self.sieve[actual] = [False, old_entry[1]//(i+1), old_entry[2]+[i+1]]
elif self.sieve[i][1] != 1:
old_entry = self.sieve[i]
self.sieve[i] = [False, 1, old_entry[2]+self.sieve[old_entry[1]-1][2]]
self._length = target