summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2018-05-25 14:13:16 +0800
committerIru Cai <mytbk920423@gmail.com>2018-05-25 14:13:16 +0800
commitd5c645bf83f144838e84b21e4941a0e42b0a9a61 (patch)
tree7164988f5dcfa2264d50f4c184f4c278f5983d27
parent3caa8876e4c1d71cf8973d0d78231f16a0cd5b93 (diff)
downloadproject_euler-d5c645bf83f144838e84b21e4941a0e42b0a9a61.tar.xz
47, 49, 50
-rw-r--r--euler47.myr49
-rw-r--r--euler49.myr53
-rw-r--r--euler50.myr80
3 files changed, 182 insertions, 0 deletions
diff --git a/euler47.myr b/euler47.myr
new file mode 100644
index 0000000..f53bec0
--- /dev/null
+++ b/euler47.myr
@@ -0,0 +1,49 @@
+use std
+
+const n_prime_factors = {n
+ var nf = 0
+ var p = 3
+
+ if n % 2 == 0
+ while n % 2 == 0
+ n /= 2
+ ;;
+ nf = 1
+ ;;
+
+ while n > 1
+ if n % p == 0
+ while n % p == 0
+ n /= p
+ ;;
+ nf ++
+ ;;
+ p += 2;
+ ;;
+ -> nf
+}
+
+
+const main = {
+ var i = 210
+ while true
+ if !(n_prime_factors(i) == 4)
+ i++
+ continue
+ ;;
+ if !(n_prime_factors(i+1) == 4)
+ i += 2
+ continue
+ ;;
+ if !(n_prime_factors(i+2) == 4)
+ i += 3
+ continue
+ ;;
+ if !(n_prime_factors(i+3) == 4)
+ i += 4
+ continue
+ ;;
+ std.put("{}\n", i)
+ break
+ ;;
+}
diff --git a/euler49.myr b/euler49.myr
new file mode 100644
index 0000000..4003767
--- /dev/null
+++ b/euler49.myr
@@ -0,0 +1,53 @@
+use std
+
+const isprime = {n
+ if n == 1 || n % 2 == 0
+ ->false
+ ;;
+ for var i = 3; i * i <= n; i++
+ if n % i == 0
+ -> false
+ ;;
+ ;;
+ -> true
+}
+
+const isperm = {a, b
+ var dmap: int[10]
+ for var i = 0; i < 10; i++
+ dmap[i] = 0
+ ;;
+ while a > 0
+ dmap[a % 10] ++
+ a /= 10
+ ;;
+ while b > 0
+ var t = b % 10
+ dmap[t] --
+ if dmap[t] < 0
+ -> false
+ ;;
+ b /= 10
+ ;;
+ -> true
+}
+
+const main = {
+ for var i = 1001; i < 9999; i++
+ if !isprime(i)
+ continue
+ ;;
+ for var j = 2; i + j * 2 <= 9999; j ++
+ var t1 = i + j
+ var t2 = t1 + j
+ if !isprime(t1) || !isprime(t2)
+ continue
+ ;;
+ if !isperm(i, t1) || !isperm(i, t2)
+ continue
+ ;;
+ std.put("{} {} {}\n", i, t1, t2)
+ ;;
+ ;;
+}
+
diff --git a/euler50.myr b/euler50.myr
new file mode 100644
index 0000000..4fd1b07
--- /dev/null
+++ b/euler50.myr
@@ -0,0 +1,80 @@
+use std
+
+var primes: int[100000]
+var nprimes: int
+
+const sieve = {
+ nprimes = 1
+ primes[0] = 2
+
+ for var i = 3; i < 1_000_000; i++
+ for var j = 0; j < nprimes; j++
+ if i % primes[j] == 0
+ break
+ ;;
+ if primes[j] * primes[j] > i
+ primes[nprimes] = i
+ nprimes++
+ break
+ ;;
+ ;;
+ ;;
+}
+
+const isprime = {n
+ for var i = 0; i < nprimes; i++
+ if n % primes[i] == 0
+ -> false
+ ;;
+ if (primes[i]*primes[i] > n)
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const prime_terms = {n, limit
+ var L = n / limit
+ var i = 0
+ var j = 0
+ var sum = 0
+
+ while primes[i] < L
+ /* calculate a[i..j] */
+ while sum < n
+ sum += primes[j]
+ if sum == n
+ -> j - i + 1
+ ;;
+ j++
+ ;;
+ /* calculate a[i..j-1] */
+ while sum > n
+ sum -= primes[i]
+ i++
+ if sum == n
+ -> j - i
+ ;;
+ ;;
+ ;;
+ -> 0
+}
+
+const main = {
+ sieve()
+ std.put("{} primes below 1000000\n", nprimes)
+ var maxL = 21
+ var ans = 953
+ for var i = 954; i < 1_000_000; i++
+ if !isprime(i)
+ continue
+ ;;
+ var pt = prime_terms(i, maxL)
+ if pt > maxL
+ maxL = pt
+ ans = i
+ ;;
+ ;;
+ std.put("len = {} ans = {}\n", maxL, ans)
+}
+