diff --git a/061_cyclical_fig_nums.py b/061_cyclical_fig_nums.py new file mode 100644 index 0000000..fdf7a52 --- /dev/null +++ b/061_cyclical_fig_nums.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +from math import sqrt + +def cynical(): + #main + i=1+int((2+sqrt(4+12*1009))/6) + o=3*i**2-2*i + while o<10000: + base=(o-(o//100)*100)*100 + if base>=1000: + a=curse(base,o//100,[]) + if a!=False: + return a+[o] + i+=1 + o=3*i**2-2*i + return False + +def curse(base,last,sit): + #recursive + if len(sit)==4: + if fig(base+last)!=False and not(fig(base*100+last) in sit): + return [base+last] + else: + i=10 + while i<100: + if fig(base+i)!=False and not(fig(base+i) in sit): + a=curse(i*100,last,sit+[fig(base+i)]) + if a!=False: + return a+[base+i] + i+=1 + return False + +def fig(x): + #pocekira ce je x katera izmed figurative stevil + if int(sqrt(x))==sqrt(x): + return 2 + k=(1+sqrt(1+24*x))/3 + if int(k)==k: + return 3 + k=sqrt(1+8*x) + if int(k)==k: + k=(1+sqrt(1+8*x))/4 + if int(k)==k: + return 4 + return 1 + k=(3+sqrt(9+40*x))/5 + if int(k)==k: + return 5 + return False + +if __name__ == '__main__': + print(sum(cynical())) diff --git a/062_cubic_permutations.py b/062_cubic_permutations.py new file mode 100644 index 0000000..81e14e0 --- /dev/null +++ b/062_cubic_permutations.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +def cube(x): + i=3 + lim=100 + while True: + cubes=[] + while i**30: + for i in sorted(ans): + if perm[ans_route[i]][0]==x: + return i + +if __name__ == '__main__': + print(cube(5)) diff --git a/064_odd_period_square_roots.c b/064_odd_period_square_roots.c new file mode 100644 index 0000000..25dcd07 --- /dev/null +++ b/064_odd_period_square_roots.c @@ -0,0 +1,39 @@ +#include +#include + +int sq_frac(int x); + +int N = 10000; + +int main(int argc, char *argv[]) +{ + int i, c; + + c = 0; + for(i = 1; i <= N; ++i){ + if(sq_frac(i) % 2 == 0){ + ++c; + } + } + printf("%d\n", c); + return 0; +} + +int sq_frac(int x) +{ + int m, n, d, a, c; + + if((m = sqrt(x)) * m == x){ + return 1; + } + d = c = 1; + n = m; + do{ + d = (x - n * n) / d; + a = (n + m) / d; + n = a * d - n; + ++c; + }while(d != 1); + return c; +} + diff --git a/065_convergents_of_e.py b/065_convergents_of_e.py new file mode 100644 index 0000000..3912451 --- /dev/null +++ b/065_convergents_of_e.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +def nth_con(x): + # 212 114 116 118 + if not x % 3: + n = 1 + d = 0 + else: + n = x % 3 + d = 1 + for m in range(2 * (x // 3), 2, -2): + n, d = 2*m*n + 2*d + n, m*n + n + d + return 8*n + 3*d, 2*n + d + n + +if __name__ == '__main__': + print(sum(map(int, list(str(nth_con(100)[0]))))) diff --git a/066_diophantine_equation.py b/066_diophantine_equation.py new file mode 100644 index 0000000..152abe4 --- /dev/null +++ b/066_diophantine_equation.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +from math import sqrt + +def main(d): + mx = 0 + my = 0 + md = 0 + for cd in range(2, d + 1): + x, y = diop(cd) + #print("{} -> {}, {}".format(cd, x, y)) + if x > mx: + mx = x + my = y + md = cd + return md, mx, my + +def diop(d): + sol = [] + a, f = sq_frac(d) + f.reverse() + if len(f) == 0: + return 0, 0 + for i in f: + for j in range(len(sol)): + sol[j][0], sol[j][1] = sol[j][0] * i + sol[j][1], sol[j][0] + sol.append([i, 1]) + r = fits(a, sol, d) + while r == 0: + for i in f: + for j in range(len(sol)): + sol[j][0], sol[j][1] = sol[j][0] * i + sol[j][1], sol[j][0] + r = fits(a, sol, d) + return r + +def fits(a, pairs, d): + for x, y in pairs[::-1]: + x, y = a * x + y, x + if x**2 - d * y**2 == 1: + return x, y + return 0 + +def sq_frac(x): + f = [] + m = int(sqrt(x)) + if m**2 == x: + return m, f + d = x - m**2 + f.append((2 * m) // d) + n = f[-1] * d - m + while d != 1: + d = (x - n**2) // d + f.append((n + m) // d) + n = f[-1] * d - n + return m, f + +if __name__ == '__main__': + print(main(1000)[0]) diff --git a/068_magic_5gon_ring.py b/068_magic_5gon_ring.py new file mode 100644 index 0000000..1634214 --- /dev/null +++ b/068_magic_5gon_ring.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +def m5gon(x, i, mx): + r = [] + m = [[] for _ in range(5)] + for a, b in enumerate(range(i-x-1, (i-x)//2, -1), 1): + if a > mx: + break + elif b > mx or a == x or b == x: + continue + m[0] = [x, a, b] + for ci, c in enumerate(range(i-b-1, 0, -1), 1): + if ci > mx: + break + elif c > mx or len(set([a, b, x, ci, c])) < 5: + continue + m[1] = [ci, b, c] + for di, d in enumerate(range(i-c-1, 0, -1), 1): + if di > mx: + break + elif d > mx or len(set([a, b, x, c, ci, d, di])) < 7: + continue + m[2] = [di, c, d] + for ei, e in enumerate(range(i-d-1, 0, -1), 1): + if ei > mx: + break + elif e > mx or len(set([a, b, x, c, ci, d, di, e, ei, i-e-a])) < 10 or i-e-a > mx: + continue + m[3] = [ei, d, e] + m[4] = [i-e-a, e, a] + st = m.index(sorted(m, key = lambda y: y[0])[0]) + m = m[st:] + m[:st] + if m[0][1] < m[0][2]: + m = list(map(lambda y: (y[0], y[2], y[1]), [m[0]] + m[::-1][:-1])) + r.append(''.join([''.join(map(str, y)) for y in m])) + return r + +if __name__ == '__main__': + print(max(m5gon(10, i, 10) for i in range(10, 25))[0]) + diff --git a/074_digit_factorial_chains.c b/074_digit_factorial_chains.c new file mode 100644 index 0000000..b949d66 --- /dev/null +++ b/074_digit_factorial_chains.c @@ -0,0 +1,84 @@ +#include +#include + +#define LIM 3 * 1000000 +#define MAX_CHAIN 100 + +void initf(int f[]); +void init_sieve(char s[], int len); +long next_term(long x, int f[]); + +int main() +{ + int x = pow(10, 6); + int y = 60; + int c, len, count; + long i, n; + long chain[MAX_CHAIN]; + int f[10]; + char sieve[LIM]; + + initf(f); + init_sieve(sieve, LIM); + count = 0; + + for(i = 3; i < x; ++i){ + c = len = 0; + n = i; + while(n >= LIM || !sieve[n]){ + if(c < MAX_CHAIN){ + chain[c] = n; + ++len; + } + n = next_term(n, f); + ++c; + } + c += sieve[n]; + if(c == y){ + ++count; + } + for(n = 0; n < len; ++n){ + if(chain[n] < LIM){ + sieve[chain[n]] = c; + } + --c; + } + + } + printf("%d\n", count); + return 0; +} + +long next_term(long x, int f[]) +{ + long r; + int i, d; + r = 0; + for(i = pow(10, (int) log10(x)); i >= 1; i /= 10){ + d = x / i; + r += f[d]; + x -= d * i; + } + return r; +} + +void initf(int f[]) +{ + int i; + f[0] = f[1] = 1; + for(i = 2; i < 10; ++i){ + f[i] = i * f[i - 1]; + } +} + +void init_sieve(char s[], int len) +{ + int i; + + for(i = 0; i < len; ++i){ + s[i] = 0; + } + s[1] = s[2] = s[145] = s[40585] = 1; + s[871] = s[45361] = s[872] = s[45362] = 2; + s[169] = s[363601] = s[1454] = 3; +} diff --git a/075_singular_integer_right_triangles.c b/075_singular_integer_right_triangles.c new file mode 100644 index 0000000..affbd01 --- /dev/null +++ b/075_singular_integer_right_triangles.c @@ -0,0 +1,42 @@ +#include +#include + +int gcd(int a, int b); + +int main(int argc, char *argv[]) +{ + int lim = 1500000; + int i, n, m, p, c, odd; + char sieve[lim + 1]; + + for(i = 0; i <= lim; ++i){ + sieve[i] = 0; + } + odd = c = 0; + for(n = 2; n <= (sqrt(2*lim + 1) - 1)/ 2; ++n){ + for(m = odd + 1; m < n && m <= (lim / 2*n) - n; m += 2){ + if(gcd(n, m) == 1){ + p = 2*n * (n + m); + for(i = p; i <= lim; i += p){ + sieve[i] += 1; + } + } + } + odd = !odd; + } + for(i = 0; i <= lim; i += 2){ + if(sieve[i] == 1){ + ++c; + } + } + printf("%d\n", c); +} + +int gcd(int a, int b) +{ + int c; + while (a != 0){ + c = a; a = b%a; b = c; + } + return b; +} diff --git a/076_counting_summations.c b/076_counting_summations.c new file mode 100644 index 0000000..393cca5 --- /dev/null +++ b/076_counting_summations.c @@ -0,0 +1,28 @@ +#include + +int sums(int x); + +int main(int argc, char *argv[]) +{ + printf("%d\n", sums(100)); +} + +int sums(int x) +{ + int i, n; + int s[x + 1][x]; + + for(n = 0; n <= x; ++n){ + s[n][0] = 1; + for(i = 2; i < n; ++i){ + s[n][i - 1] = s[n][i - 2]; + if(n - i <= i){ + s[n][i - 1] += s[n - i][n - i - 1]; + }else{ + s[n][i - 1] += s[n - i][i - 1]; + } + } + s[n][i - 1] = s[n][i - 2] + 1; + } + return s[x][x - 1] - 1; +} diff --git a/077_prime_summations.py b/077_prime_summations.py new file mode 100644 index 0000000..d20085d --- /dev/null +++ b/077_prime_summations.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from math import sqrt + +def main(lim, x): + sieve = [False, True] * (lim//2 + 1) + primes = [] + sub_lim = int(sqrt(lim) + 1) + count = [[1], [0], [1]] + even = False + for n in range(3, lim + 1): + if sieve[n]: + primes.append(n) + for j in range(n**2, lim + 1, 2*n): + sieve[j] = False + count.append([int(even)]) + for i, p in enumerate(primes, 1): + if p > n - p: + count[n].append(count[n - p][-1] + count[n][-1]) + else: + count[n].append(count[n - p][i] + count[n][-1]) + if count[n][-1] - int(sieve[n]) > x: + return n, count[n][-1] - int(sieve[n]) + even = not even + return False + +if __name__ == '__main__': + lim = 10**6 + x = 5000 + print(main(lim, x)[0]) diff --git a/078_coin_partitions.py b/078_coin_partitions.py new file mode 100644 index 0000000..07a10b4 --- /dev/null +++ b/078_coin_partitions.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +def main(d): + tbl = [1] + n = 1 + while p(n, tbl) % d: n+=1 + return n, p(n,tbl) + +def p(n, tbl=[1]): + if n < 0: + return 0 + if n >= len(tbl): + r = -sum((-1)**i * + (p(n-a, tbl) + + p(n-b, tbl)) + for a,b,i in pentnum(n)) + tbl.append(r) + return tbl[n] + +def pentnum(bound): + n = 1 + a,b = 1,2 + while a <= bound: + yield a,b,n + a += 3*n + 1 + b += 3*n + 2 + n += 1 + +print(main(10**6)[0]) diff --git a/makefile b/makefile index 6424554..be1cf88 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,18 @@ +all : 064 074 075 076 085 -85 : 085_counting_rectangles.c +064 : 064_odd_period_square_roots.c + gcc 064_odd_period_square_roots.c -lm -o 064_odd_period_square_roots.exe + +074 : 074_digit_factorial_chains.c + gcc 074_digit_factorial_chains.c -lm -o 074_digit_factorial_chains.exe + +075 : 075_singular_integer_right_triangles.c + gcc 075_singular_integer_right_triangles.c -lm -o 075_singular_integer_right_triangles.exe + +076 : 076_counting_summations.c + gcc 076_counting_summations.c -o 076_counting_summations.exe + +085 : 085_counting_rectangles.c gcc 085_counting_rectangles.c -lm -o 085_counting_rectangles.exe - clean : rm -f *.exe *.out