Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Re: Euler probl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 4021 of 4173
Post > Topic >>

Re: Euler problem #20

by William James <w_a_x_man@[EMAIL PROTECTED] > Apr 27, 2008 at 12:16 PM

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
 




 3 Posts in Topic:
Euler problem #20
mhx@[EMAIL PROTECTED] (M  2008-04-27 07:24:33 
Re: Euler problem #20
William James <w_a_x_m  2008-04-27 12:16:20 
Re: Euler problem #20
mhx@[EMAIL PROTECTED] (M  2008-04-27 22:34:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 22:26:53 CDT 2008.