Talk About Network



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 4 of 4 Topic 4041 of 4065
Post > Topic >>

Re: Euler problem #63

by Albert van der Horst <albert@[EMAIL PROTECTED] > May 8, 2008 at 04:58 PM

In article
<67930447-88f9-4fdc-bd1a-8ca096d441bf@[EMAIL PROTECTED]
>,
William James  <w_a_x_man@[EMAIL PROTECTED]
> wrote:
>On May 5, 11:49 am, an...@[EMAIL PROTECTED]
 (Anton Ertl)
>wrote:
>> Still small enough to solve in a few lines:
>>
>> http://www.complang.tuwien.ac.at/forth/programs/euler/63.fs
>>
>> \ Problem:
>> \ The 5-digit number, 16807=75, is also a fifth power. Similarly, the
>> \ 9-digit number, 134217728=89, is a ninth power.
>>
>> \ How many n-digit positive integers exist which are also an nth power?
>> \ They don't count 0^1 (0 is not deemed to be positive)
>>
>> \ Solution:
>> \ 10^n produces a n+1-digit integer, so we need only look at bases 0-9.
>>
>> : n-digits { n -- n2 }
>>     \ number of n-th powers with n digits
>>     \ for n=1, it does not count 0 and 1 (I correct for the 1 below)
>>     \ this works by just seeing what the lowest x is that fits in
>>     \ 10^(n-1), and then rounding x up to an integer
>>     n 1- 0 d>f n 0 d>f f/ falog f>d drop 9 swap - ;
>
>Clever, but the stack makes it hard to follow the code.
>
>>
>> : solve ( -- )
>>     1 1 begin ( n sum )
>>         over n-digits dup 0> while
>>             + swap 1+ swap repeat
>>     drop nip ;
>
>Again, the stack makes it very difficult to follow.
>When one sees the +, he can't easily tell what two
>items are being added together.  This code is hard
>to understand and to modify.
>
>Ruby:
>
>def n_digits n
>  9 - ( 10 ** ((n-1.0) / n) ).to_i
>end
>
>sum = 1
>(1 .. 9999).each{|i|
>  n = n_digits(i)
>  break if n < 1
>  sum += n }
>p sum

Please don't publish spoilers without the words spoiler in
the subject line.

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@[EMAIL PROTECTED]
 &=n http://home.hccnet.nl/a.w.m.van.der.horst




 4 Posts in Topic:
Euler problem #63
anton@[EMAIL PROTECTED]   2008-05-05 16:49:30 
Re: Euler problem #63
William James <w_a_x_m  2008-05-07 11:45:18 
Re: Euler problem #63
anton@[EMAIL PROTECTED]   2008-05-07 20:50:24 
Re: Euler problem #63
Albert van der Horst <  2008-05-08 16:58:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Sat May 17 4:02:48 CDT 2008.