-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
77 lines (72 loc) · 2.08 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const {decide_type, sum_of_palindromes, big_sum, is_palindrome, digits_of, debugging} = require('./3palindromes.js');
function test(n,base=10) {
let palindromes = [];
try {
palindromes = sum_of_palindromes(n,base);
if(!palindromes.every(is_palindrome)) {
throw(new Error("Not every number is a palindrome"));
}
if(!palindromes.every(p=>{
for(let i=0;i<p.length;i++) {
if(p[i]===undefined || p[i]===null) {
return false;
}
}
return true;
})) {
throw(new Error("Not every digit is present"));
}
if(!palindromes.every(p=>p.every(d => d>=0 && d<base))) {
throw(new Error("Not every digit is valid"));
}
const digits = digits_of(n,base).reverse();
const t = big_sum(palindromes);
for(let i=0;i<digits.length;i++) {
if(digits[i]!=t[i]) {
console.log('total',t.reverse().join(''));
throw(new Error("Doesn't sum to the same thing"));
}
}
} catch(e) {
console.error(`n: "${n}"`);
console.log(palindromes.join('\n'));
console.error(e.message);
return false;
}
return true;
}
const bads = [
10321393
];
const arg = process.argv[2] || 'every';
if(!isNaN(arg)) {
debugging.active = true;
if(test(parseInt(arg))) {
console.log('works');
}
} else if(arg=='every') {
const from = parseInt(process.argv[3] || 0);
const limit = parseInt(process.argv[4] || 10**8);
let marker = Math.pow(2,Math.floor(Math.log(from)/Math.log(2)));
console.log(marker);
for(let i=from;i<limit;i++) {
if(i==marker) {
console.log(marker);
marker *= 2;
}
if(!test(i)) {
break;
}
}
} else if(arg=='bads') {
debugging.active = true;
for(let n of bads) {
console.log('\n\n');
console.log(n);
if(!test(n)) {
break;
} else {
console.log("YEP",n);
}
}
}