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; }