diff options
author | Iru Cai <mytbk920423@gmail.com> | 2018-05-26 16:12:24 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2018-05-26 16:12:24 +0800 |
commit | 1c76beb0d4ccf57d2db84cf417f37a3a155fb905 (patch) | |
tree | 310bbc9bc48b2860f42b9994a62c38a1b91829b8 | |
parent | c98d66aca04d0d839833b6b81bf249822c4ca350 (diff) | |
download | project_euler-1c76beb0d4ccf57d2db84cf417f37a3a155fb905.tar.xz |
58, 76, 97, 104
-rw-r--r-- | euler104.py | 35 | ||||
-rw-r--r-- | euler58.c | 45 | ||||
-rw-r--r-- | euler76.scm | 17 | ||||
-rw-r--r-- | euler97.c | 30 |
4 files changed, 127 insertions, 0 deletions
diff --git a/euler104.py b/euler104.py new file mode 100644 index 0000000..5ec15b4 --- /dev/null +++ b/euler104.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +from math import log10,sqrt + +fn_1 = 1 +fn_2 = 1 +n = 3 + +def pandigital(n): + a = [False] * 10 + while n > 0: + t = n % 10 + if t == 0: + return False + if a[t]: + return False + a[t] = True + n = n / 10 + return True + +while True: + fn = (fn_1 + fn_2) % 1000000000 + if fn > 100000000 and pandigital(fn): + # a[n] = pow((1+sqrt(5))/2, n)/sqrt(5) + # - pow((1-sqrt(5))/2, n)/sqrt(5) + log10_fn = n*log10((1+sqrt(5))/2) - log10(sqrt(5)) + frac = log10_fn - int(log10_fn) + first9 = int(pow(10, frac)*100000000) + if pandigital(first9): + print(n) + break + + fn_2 = fn_1 + fn_1 = fn + n = n + 1 diff --git a/euler58.c b/euler58.c new file mode 100644 index 0000000..ac46073 --- /dev/null +++ b/euler58.c @@ -0,0 +1,45 @@ +#include <stdio.h> + +/* + * delta = 4n+4 delta = 4n+2 + * 5 3 + * [1] + * 7 9 + * delta = 4n+6 delta = 4n+8 + */ + +int isprime(int n) +{ + if (n == 1 || n % 2 == 0) + return 0; + + for (int i = 3; i * i <= n; i += 2) { + if (n % i == 0) + return 0; + } + + return 1; +} + +int main() +{ + int ndiag = 5; + int nprime = 3; + int s[4] = {3, 5, 7, 9}; + int n = 2; + + while (nprime * 10 > ndiag) { + s[0] += n * 4 + 2; + s[1] += n * 4 + 4; + s[2] += n * 4 + 6; + s[3] += n * 4 + 8; + for (int i = 0; i < 4; i++) { + if (isprime(s[i])) + nprime++; + } + ndiag += 4; + n += 2; + } + int sidelen = n + 1; + printf("%d\n", sidelen); +} diff --git a/euler76.scm b/euler76.scm new file mode 100644 index 0000000..2542356 --- /dev/null +++ b/euler76.scm @@ -0,0 +1,17 @@ +(define (sum_ways n) + (define (sw n min-n) + (cond + [(> min-n n) 0] + [(> (* min-n 2) n) 1] + [else (+ (sw (- n min-n) min-n) (sw n (+ min-n 1)))] + ) + ) + + (define (sw-iter sum i) + (if (> (* i 2) n) sum + (sw-iter (+ sum (sw (- n i) i)) (+ i 1)))) + + (sw-iter 0 1)) + +(display (sum_ways 100)) +(newline) diff --git a/euler97.c b/euler97.c new file mode 100644 index 0000000..b0f55bc --- /dev/null +++ b/euler97.c @@ -0,0 +1,30 @@ +#include <openssl/bn.h> + +static const unsigned char _7830457[3] = { 0x77, 0x7b, 0xb9 }; +static const unsigned char _2[1] = {2}; +static const unsigned char _28433[2] = {0x6f, 0x11}; +static const unsigned char _10000000000[5] = {0x02, 0x54, 0x0b, 0xe4, 0x00}; + +int main() +{ + BIGNUM *b28433 = BN_bin2bn(_28433, 2, NULL); + BIGNUM *b2 = BN_bin2bn(_2, 1, NULL); + BIGNUM *b7830457 = BN_bin2bn(_7830457, 3, NULL); + BIGNUM *b10ten = BN_bin2bn(_10000000000, 5, NULL); + + BN_CTX *ctx = BN_CTX_new(); + BIGNUM *r = BN_new(); + BIGNUM *r2 = BN_new(); + BIGNUM *r3 = BN_new(); + BN_mod_exp(r, b2, b7830457, b10ten, ctx); + BN_mul(r2, r, b28433, ctx); + BN_add(r3, r2, BN_value_one()); + BN_mod(r, r3, b10ten, ctx); + + unsigned char res[10]; + int len = BN_bn2mpi(r, res); + for (int i = 0; i < len; i++) + printf("%02x", res[i]); + + puts(""); +} |