-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhappycoin.js
More file actions
46 lines (33 loc) · 779 Bytes
/
Copy pathhappycoin.js
File metadata and controls
46 lines (33 loc) · 779 Bytes
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
import { randomFillSync } from "node:crypto";
const array = new BigUint64Array(1);
function random64() {
randomFillSync(array);
return array[0];
}
function sumDigitsSquared(num) {
let total = 0n;
while (num > 0) {
const numModBase = num % 10n;
total += numModBase ** 2n;
num /= 10n;
}
return total;
}
function isHappy(num) {
while (num !== 1n && num !== 4n) {
num = sumDigitsSquared(num);
}
return num === 1n;
}
function isHappyCoin(num) {
return isHappy(num) && num % 10_000n === 0n;
}
let count = 0;
for (let i = 1; i < 10_000_000; i++) {
const randomNumber = random64();
if (isHappyCoin(randomNumber)) {
process.stdout.write(`${randomNumber.toString()} `);
count++;
}
}
process.stdout.write(`\ncount: ${count}`);