Project Euler: Problem 20

node v8.17.0
version: 2.0.1
endpointsharetweet
https://projecteuler.net/problem=20 n! means n × (n − 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100!
const { Stack } = require('@hypercubed/f-flat'); const f = new Stack(); f.eval(` core: rcl use math: rcl use 160 set-precision `).stack;
We create a new f-flat stack environment, bring the math and core modules into sopce, and set the precision to 200 places (100! is a 157 digit value).
f.eval(` ( 100 ! 0 [ swap 10 divrem ] 160 times ) sum `).stack[0].valueOf();
Inside an list, calulate the factorial of 100. Then divrem by 10 (returns the result and remainder of integer division) 200 times to retreive each digit. Then sum the list.
Loading…

no comments

    sign in to comment