Marcel Hendrix wrote:
> Apparently, there is a rather uncomplicated way of generating and
printing
> very large numbers. The 20th Euler problem is supposed to be rather
easy.
>
> Of course most Forth will have bignum libraries, but it seems overkill
> for this problem.
>
> There is another one where they want the sum of digits of 2^1000.
>
> Ideas?
>
> -marcel
> -- ----------------------------------
> INCLUDE ../bignum/bignum.frt
I'll do it without "include" or "require".
>
> (*
> ( Collection at http://projecteuler.net/index.php?section=problems
)
>
> n! means n * (n - 1) * ... * 3 * 2 * 1
>
> Find the sum of the digits in the number 100!
>
> Example output:
>
> FORTH> euler20
> 100! = 93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,
> 264,381,621,468,592,963,895,217,599,993,229,915,608,941,463,976,
> 156,518,286,253,697,920,827,223,758,251,185,210,916,864,000,000,
> 000,000,000,000,000,000
> The sum of the digits of the number 100! = 648
> 0.002 seconds elapsed. ok
> *)
>
> MAX.DIGITS BIGNUM n!
>
> : Euler20 ( -- )
> 1 n! V! #101 2 DO n! I VS* LOOP
> CR ." 100! = " n! .Vnum
> CR ." The sum of the digits of the number 100! = "
> 0 BEGIN
> n! #10 VS/MOD +
> n! VS0=
> UNTIL
> 0 D. ;
>
> : .ABOUT CR ." Euler20 -- Finds the sum of the digits in the number
100!" ;
>
> .ABOUT
Ruby:
n = 1
(2..100).each{|i| n *= i }
s = n.to_s
puts s.reverse.gsub( /.../, '\&,' ).reverse
sum = 0
s.split('').each{|c| sum += c.to_i }
puts sum
--- output ---
93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,
264,381,621,468,592,963,895,217,599,993,229,915,608,941,463,
976,156,518,286,253,697,920,827,223,758,251,185,210,916,864,
000,000,000,000,000,000,000,000
648


|