This commit is contained in:
John Alanbrook 2024-03-06 18:55:45 -06:00
commit eea5337024
33 changed files with 854 additions and 0 deletions

27
11.js Normal file
View file

@ -0,0 +1,27 @@
import * as std from "std";
var data = std.loadFile("11.txt").trim().split('n');
for (var i = 0; i < data.length; i++) {
data[i] = data[i].split(' ');
data[i] = data[i].map(x=>Number(x));
}
var high = 0;
data.forEach(function(line) {
for (var i = 0; i < line.length-4; i++) {
var n = line.slice(i,i+4).reduce((a,c) => a *= c);
high = n > high ? n : high;
}
});
for (var i = 0; i < data[0].length-4; i++) {
for (var j = 0; j < data[0].length; j++) {
console.log(`${i},${j}`);
var n = data[i][j]*data[i+1][j]*data[i+2][j]*data[i+3][j];
console.log(`${i},${j}`);
high = n > high ? n : high;
}
}
console.log(high);

20
11.txt Normal file
View file

@ -0,0 +1,20 @@
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 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 03 45 02 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 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 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 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

11
12.js Normal file
View file

@ -0,0 +1,11 @@
import * as euler from 'euler.js'
var i = 1;
var c = 0;
while (true) {
c += i++;
if (euler.divisors(c).length > 500) {
console.log(c);
break;
}
}

25
14.js Normal file
View file

@ -0,0 +1,25 @@
var high_c = 1;
var high = 1;
var collatz = function(n)
{
if (n%2===0)
return n/2;
return 3*n+1;
}
for (var i = 2; i < 1000000; i++) {
var c = 1;
var n = i;
while (n !== 1) {
n = collatz(n);
c++;
}
if (c > high_c) {
high_c = c;
high = i;
}
}
console.log(high);

10
15.js Normal file
View file

@ -0,0 +1,10 @@
"use math";
import * as euler from 'euler.js'
function lattice_moves(n)
{
return euler.factorial(n*2)/(euler.factorial(n)*euler.factorial(n));
}
console.log(lattice_moves(20));

7
16.js Normal file
View file

@ -0,0 +1,7 @@
"use math";
var n = 1;
for (var i = 0; i < 1000; i++)
n *= 2;
var s = String(n).split('').map(x=>Number(x)).reduce((a,i)=>a+=i);
console.log(s);

80
17.js Normal file
View file

@ -0,0 +1,80 @@
var numstrs = [];
var nstr = {
0: '',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten',
11: 'eleven',
12: 'twelve',
13: 'thirteen',
14: 'fourteen',
15: 'fifteen',
16: 'sixteen',
17: 'seventeen',
18: 'eighteen',
19: 'nineteen',
20: 'twenty',
30: 'thirty',
40: 'forty',
50: 'fifty',
60: 'sixty',
70: 'seventy',
80: 'eighty',
90: 'ninety',
100: 'hundred',
1000: 'thousand'
1000000: 'million',
100000000: 'billion',
100000000000: 'trillion',
};
var tens2str = function(n) {
if (n > 99) return "";
if (n <= 20) return nstr[n];
var s = String(n).split('');
return nstr[Number(s[0])*10] + '-' + nstr[s[1]];
}
var num2str = function(n)
{
if (n === 0) return 'zero';
var s = String(n).split('');
var tens = s.slice(s.length-2, s.length);
var rest = s.slice(0, s.length-2).reverse();
rest = rest.map(function(v,i) {
if (v === '0') return "";
return nstr[v] + " " + nstr[Math.pow(10,i+2)] + " and ";
});
rest = rest.reverse().filter(x=>x).join('');
rest += tens2str(Number(tens.join('')));
rest = rest.replace(/ and $/, '');
rest = rest.replace(/-$/,'');
return rest;
}
var lettersinnum = function(n)
{
var str = num2str(n).replaceAll(/[- ]/g,'');
return str.length;
}
var sum = 0;
for (var i = 1; i <= 1000; i++) {
console.log(num2str(i));
sum += lettersinnum(i);
}
console.log(sum);

8
20.js Normal file
View file

@ -0,0 +1,8 @@
"use math";
var n = 1;
for (var i = 100; i > 0; i--)
n *= i;
console.log(n);
var s = String(n).split('').map(x=>Number(x)).reduce((a,i)=>a+=i);
console.log(s);

24
21.js Normal file
View file

@ -0,0 +1,24 @@
import * as euler from 'euler.js'
var divsum = function(n) { return euler.divisors(n).reduce((y,i) => y += i); }
var amicable = function(a)
{
var b = divsum(a);
if (divsum(b) === a) return b;
return undefined;
}
var ams = [];
for (var i = 1; i < 10000; i++) {
var am = amicable(i);
if (am && am !== i)
ams.push(i, am);
}
function unique(v,i,a) { return a.indexOf(v) === i; }
ams = ams.filter(unique);
console.log(ams);
console.log(ams.reduce((y,x) => y+=x));

15
22.js Normal file
View file

@ -0,0 +1,15 @@
import * as std from 'std';
var names = std.loadFile('22_names.txt').replaceAll(/"/g, '').split(',');
names = names.sort();
var sum = 0;
names.forEach(function(n,i) {
var score = 0;
n = n.toUpperCase().split('').map(c => c.charCodeAt(0));
n.forEach(c => score += (c-64));
sum += (score*(i+1));
});
console.log(sum);

1
22_names.txt Normal file

File diff suppressed because one or more lines are too long

7
23.js Normal file
View file

@ -0,0 +1,7 @@
import * as euler from 'euler.js'
var perfect_number = function(n)
{
var s = euler.divisors(n).reduce((a,n)=>a+=n);
return n === s;
}

17
24.js Normal file
View file

@ -0,0 +1,17 @@
import * as euler from 'euler.js'
var nums = [0,1,2,3,4,5,6,7,8,9,];
var tar = 1000000;
var a = "";
for (var i = 0; i < 10; i++) {
var f = euler.factorial(9-i);
var n = Math.floor(tar/f);
tar = tar%f;
console.log(n);
a += String(nums[n]);
nums = nums.slice(0,n).concat(nums.slice(n+1));
// console.log(nums);
}
console.log(a);

28
25.js Normal file
View file

@ -0,0 +1,28 @@
var fibonacci = function(n)
{
if (fibonacci.cache[n]) return fibonacci.cache[n];
if (n < 2) return n;
var f = 0l + fibonacci(n-1) + fibonacci(n-2);
fibonacci.cache[n] = f;
return f;
}
fibonacci.cache = {};
var n = 1l;
var s = fibonacci(n);
var digits = function(n)
{
var s = String(n.toExponential(0));
s = s.match(/e.*/)[0].match(/\d+/)[0];
return Number(s)+1;
}
while (digits(s) < 1000) {
n++;
s = fibonacci(n);
}
console.log(n);

111
26.js Normal file
View file

@ -0,0 +1,111 @@
"use math";
var longest_repeating = function(s)
{
var n = s.length;
var LCSRe = new Array(n+1);
for (var i = 0; i < n+1; i++) LCSRe[i] = new Array(n+1);
for (var i = 0; i < n+1; i++)
for (var j = 0; j < n+1; j++)
LCSRe[i][j] = 0;
var res = "";
var res_len = 0;
var i = 0;
var index = 0;
for (i = 1; i <= n; i++) {
for (var j = i+1; j <= n; j++) {
if (s[i-1] === s[j-1] && LCSRe[i-1][j-1] < (j-1)) {
LCSRe[i][j] = LCSRe[i-1][j-1]+1;
if (LCSRe[i][j] > res_len) {
res_len = LCSRe[i][j];
index = Math.max(i,index);
}
} else
LCSRe[i][j] = 0;
}
}
if (res_len > 0)
for (i = index-res_len+1; i <= index; i++)
res += s.charAt(i-1);
return res;
}
var longest_hash = function(s)
{
var search = function(L, n, s) {
var seen = {};
var tmp;
for (var start = 0; start < n-L+1; start++) {
tmp = s.slice(start,start+L);
if (seen[tmp]) return start;
seen[tmp] = 1;
}
return -1;
}
var n = s.length;
var l = 1;
var r = n;
var L;
while (l<=r) {
L = r;
if (search(L,n,s) == -1)
r = L-1;
else
l = L+1;
}
return l-1;
}
var high = 0;
var high_len = 0;
var longest = function(s, min)
{
if (s.length === 1) return 1;
var sa = [];
for (var i = 0; i < s.length; i++)
sa.push(s.slice(i));
sa = sa.sort();
var ll = 0;
var lstr = "";
for (var i = 0; i < s.length-1; i++) {
var l = 0;
while (sa[i][l] === sa[i+1][l])
l++;
ll = Math.max(ll,l);
lstr = sa[i].slice(0,l);
}
return lstr;
}
var cycle = function(b)
{
var hash = {};
var a = 1;
var t = 0;
do {
hash[a] = t;
a = a%b*10;
t++;
} while (hash[a] === undefined);
return t-hash[a];
}
var high = 0;
var highlen = 0
for (var i = 1; i < 1000; i++) {
var len = cycle(i);
if (len > highlen) {
highlen = len;
high = i;
}
}
console.log(high);

35
27.js Normal file
View file

@ -0,0 +1,35 @@
import * as euler from 'euler.js';
function euler39(n) {
if (n < 0 || n > 39) return undefined;
return (n*n) + n + 41;
}
function euler79(n) {
if (n < 0 || n > 79) return undefined;
return (n*n)-79*n+1601;
}
function quadratic(a,b,c,x) {
return x*x*a + x*b + c;
}
var high = 0;
var prod = 0;
for (var a = -999; a < 1000; a++) {
for (var b = -1000; b <= 1000; b++) {
var n = 0;
var p = quadratic(1,a,b,n);
while (euler.isprime(p)) {
n++;
p = quadratic(1,a,b,n);
}
if (n > high) {
high = n;
prod = a*b;
}
}
}
console.log(prod);

13
29.js Normal file
View file

@ -0,0 +1,13 @@
var nums = [];
for (var a = 2; a <= 100; a++)
for (var b = 2; b <= 100; b++)
nums.push(Math.pow(a,b));
nums = nums.sort(function(a,b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
nums = nums.filter((x,i) => nums.indexOf(x) === i);
console.log(nums.length);

17
3.js Normal file
View file

@ -0,0 +1,17 @@
import * as euler from 'euler.js';
var tar = 600851475143;
var star = Math.sqrt(tar);
var i = 0;
var l = 0;
while (i < star)
{
if (euler.isprime(i) && tar%i === 0)
l = i;
i++;
}
console.log(l);

13
30.js Normal file
View file

@ -0,0 +1,13 @@
import * as euler from 'euler.js'
var nums = [];
/* less than 9^5 * 6 */
for (var i = 10; i < 354295; i++) {
var d = euler.digitize(i);
if (i === d.reduce((a,v) => a += v*v*v*v*v, 0))
nums.push(i);
}
console.log(nums);
console.log(nums.reduce((a,v)=>a+=v));

30
32.js Normal file
View file

@ -0,0 +1,30 @@
import * as euler from 'euler.js'
function is_pandigital(n)
{
var s = String(n);
var nums = s.split('').sort();
return nums.every((x,i) => x == i+1);
}
var prods = [];
a = 594;
for (var a = 1; a < 10000; a++)
for (var b = 2; b < Math.sqrt(a)+1; b++) {
var c = a*b;
var digs = euler.digitize(a).concat(euler.digitize(b)).concat(euler.digitize(c)).sort();
if (digs.length !== 9) continue;
if (digs.every((x,i) => x == i+1)) prods.push(c);
}
prods = prods.sort((a,b)=>a>=b?1:-1);
var sum = 0;
for (var i = 0; i < prods.length; i++)
sum += prods[i];
prods.forEach(p=>console.log(p));
console.log(sum);

12
34.js Normal file
View file

@ -0,0 +1,12 @@
import * as euler from 'euler.js'
var nums = [];
console.log(145+40585);
for (var i = 0; i < 100000; i++) {
var a = euler.digitize(i).reduce((a,v) => a += euler.factorial(v),0);
if (a === i) nums.push(i);
}
console.log(nums);

39
35.js Normal file
View file

@ -0,0 +1,39 @@
import * as euler from 'euler.js';
function rotate_num(n)
{
var digits = euler.digits(n);
return Math.trunc((n%10)*Math.pow(10,digits-1)+(n/10));
}
function num_rotated(n)
{
var rot = [n];
var num = rotate_num(n);
var digits = euler.digits(n);
while (num !== n && euler.digits(num) === digits) {
rot.push(num);
num = rotate_num(num);
}
return rot;
}
function circular_prime(p)
{
var rot = num_rotated(p);
return rot.every(n => primes[n]);
}
var count = 0;
var primes = {};
for (var i = 0; i < 1000000; i++)
if (euler.isprime(i)) primes[i] = true;
Object.keys(primes).forEach(function(p) {
p = Number(p);
if (circular_prime(p)) count++;
});
console.log(count);

30
36.js Normal file
View file

@ -0,0 +1,30 @@
function s_palindrome(s) { return s.split('').reverse().join('') === s; }
function n_reverse(n)
{
var rev = 0;
while (n) {
rev = 10*rev + n%10;
n = n/10|0;
}
return rev;
}
function is_palindrome(n)
{
var s = String(n);
return s.split('').reverse().join('') === s;
}
function db_palindrome(n)
{
var bs = n.toString(2);
return n_reverse(n) === n && s_palindrome(bs);
}
var sum = 0;
for (var i = 0; i < 1000000; i++)
if (db_palindrome(i)) sum += i;
console.log(sum);

31
37.js Normal file
View file

@ -0,0 +1,31 @@
import * as euler from 'euler.js'
function trunc_l(n) {
if (n < 10) return undefined;
return Number(euler.digitize(n).slice(1).join(''));
}
function trunc_r(n)
{
if (n < 10) return undefined;
var s = euler.digitize(n);
return Number(s.slice(0,s.length-1).join(''));
}
var truncs = [];
outer: for (var i = 10; i < 1000000; i++) {
if (euler.isprime(i)) {
var r = i;
while (r = trunc_r(r))
if (!euler.isprime(r)) continue outer;
var l = i;
while (l = trunc_l(l))
if (!euler.isprime(l)) continue outer;
truncs.push(i);
}
}
console.log(truncs);
console.log(truncs.reduce((a,v) => a += v));

21
38.js Normal file
View file

@ -0,0 +1,21 @@
import * as euler from 'euler.js'
var pans = [];
outer:for (var i = 1; i < 10000; i++) {
var digits = euler.digits(i);
var c = "";
for (var j = 1; j < Math.ceil(9/digits)+1; j++) {
c += String(j*i);
if (c.length > 9) continue outer;
if (c.length === 9 && euler.is_pandigital(c)) {
console.log(c);
pans.push(Number(c));
continue outer;
}
}
}
pans = pans.sort((a,b)=> a >=b ? 1 : -1);
console.log(pans);
console.log(pans[0]);
console.log(pans[pans.length-1]);

11
4.js Normal file
View file

@ -0,0 +1,11 @@
var n1 = 999;
var n2 = 999;
var high = 0;
var num_reverse = function(n) { return Number(String(n).split('').reverse().join('')); }
for (var n1 = 999; n1 >= 0; n1--)
for (var n2 = 999; n2 > 0; n2--)
if (num_reverse(n1*n2)===n1*n2)
high = high > n1*n2 ? high : n1*n2;
console.log(high);

21
40.js Normal file
View file

@ -0,0 +1,21 @@
import * as euler from 'euler.js'
var champernowne = function(n) {
if (n < 0) return undefined;
n--;
var c = 1;
var d = euler.digits(c);
while (n >= d) {
n -= d;
c++;
d = euler.digits(c);
}
return euler.digitize(c)[n];
}
var c = 1;
for (var i = 1; i <= 1000000; i*=10)
c *= champernowne(i);
console.log(c);

9
41.js Normal file
View file

@ -0,0 +1,9 @@
import * as euler from 'euler.js'
var pans = [];
for (var i = 1; i < 10000000; i++) {
if (!euler.isprime(i)) continue;
if (euler.is_pandigital(i)) pans.push(i);
}
console.log(pans[pans.length-1]);

21
46.js Normal file
View file

@ -0,0 +1,21 @@
import * as euler from './euler.js'
var i = 0;
var primes = [];
outer:for (var i = 2; i < 1000000; i++) {
if (euler.isprime(i)) {
primes.push(i);
continue;
}
if (i%2 === 0) continue;
for (var p = 0; p < primes.length; p++) {
var pr = primes[p];
for (var j = 1; j < Math.sqrt(i); j++) {
if (pr + (2*j*j) === i) continue outer;
}
}
console.log(i);
break;
}

11
48.js Normal file
View file

@ -0,0 +1,11 @@
"use math";
var sum = 0;
for (var i = 1; i <= 1000; i++) {
var s = i;
for (var n = 1; n < i; n++)
s *= i;
sum += s;
}
console.log(sum);

20
49.js Normal file
View file

@ -0,0 +1,20 @@
import * as euler from './euler.js'
function is_prime_perm(n)
{
if (!euler.isprime(n)) return false;
var perms = euler.permutate_num(n);
var c = 0;
for (var j = 0; j < perms.length; j++)
if (euler.isprime(perms[j])) c++;
if (c >= 3) return true;
return false;
}
var p = [];
outer:for (var i = 1000; i <= 9999; i++)
if (is_prime_perm(i)) p.push(euler.permutate_num(i));
p.forEach(z=>console.log(z));

7
9.js Normal file
View file

@ -0,0 +1,7 @@
outer: for (var c = 999; c > 3; c--)
for (var b = c-1; b > 2; b--)
for (var a = b-1; a > 1; a--)
if (a*a+b*b===c*c && a+b+c === 1000) {
console.log(a*b*c);
break outer;
}

122
euler.js Normal file
View file

@ -0,0 +1,122 @@
export var isprime = function(num)
{
if (num == 2 || num == 3)
return true;
if (num <= 1 || num % 2 == 0 || num % 3 == 0)
return false;
for (let i = 5; i * i <= num ; i+=6) {
if (num % i == 0 || num % (i + 2) == 0)
return false;
}
return true;
}
export function isodd(n) { return n%2; }
export function iseven(n) { return !isodd(n); }
export var divisors = function(n)
{
var d = [];
var high = Math.sqrt(n);
for (var i = 2; i < high; i++)
if (n%i === 0)
d = d.concat([i, n/i]);
d.push(1);
return d;
}
export function rotate_num(n)
{
var dig = digits(n);
return Math.trunc((n%10)*Math.pow(10,dig-1)+(n/10));
}
function arr_swap(A,i,j)
{
var a = A[i];
var b = A[j];
A[i] = b;
A[j] = a;
}
export function permutate_num(n)
{
var perms = [];
function arr_gen(k, A)
{
if (k === 1) {
perms.push(A.slice());
return;
}
arr_gen(k-1, A);
for (var i = 0; i < k-1; i++) {
if (k%2 === 0)
arr_swap(A,i,k-1);
else
arr_swap(A,0,k-1);
arr_gen(k-1,A);
}
}
var d = digitize(n);
d = d.map(x=>String(x));
arr_gen(d.length, d);
perms = perms.map(function(p) {
var n = "";
for (var i = 0; i < p.length; i++)
n += p[i];
return Number(n);
});
return perms;
}
export function is_pandigital(n)
{
var s = n;
if (typeof s === 'number')
s = String(s);
var nums = s.split('').sort();
return nums.every((x,i) => x == i+1);
}
export var digitize = function(n)
{
var d = [];
while (n) {
d.push(n%10);
n = Math.trunc(n/10);
}
return d.reverse();
}
export var digits = function(n) { return Math.floor(Math.log10(Math.abs(n)))+1; }
export var factorial = function(n)
{
var s = 1;
for (var i = n; i > 0; i--)
s *= i;
return s;
}
export var combination = function(n,k) { return factorial(n)/(factorial(k)*factorial(n-k)); }
export var permutation = function(n,k) { return factorial(n)/factorial(n-k); }
export function euler39(n) {
if (n < 0 || n > 39) return undefined;
return (n*n) + n + 41;
}
export function euler79(n) {
if (n < 0 || n > 79) return undefined;
return (n*n)-79*n+1601;
}