Google Groups Home
Help | Sign in
Euler problem #48
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Marcel Hendrix  
View profile
 More options May 7, 4:55 pm
Newsgroups: comp.lang.forth
From: m...@iae.nl (Marcel Hendrix)
Date: Wed, 7 May 2008 22:55:35 +0200
Local: Wed, May 7 2008 4:55 pm
Subject: Euler problem #48
        INCLUDE ../bignum/factor.frt

(*
  The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.

  Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
*)

: INIT   ( -- )   S" 10000000000" s2 >GIANT  S" 0" s3 >GIANT ;
: TERM   ( n -- ) DUP 0 <# #S #>  s1 >GIANT  s1 SWAP s2 GS^MOD  s3 s1 GG+ ;
: 1000T  ( -- g ) INIT #1001 1 DO  I TERM  LOOP  s3 s2 GGMOD s3 .GIANT ;

: Euler48 ( -- )  
         CR ." The last ten digits of the series 1^1 + 2^2 + 3^3 + ... + 1000^1000 are " 1000T ;

: .ABOUT CR ." Euler48 -- Find the last ten digits of the series, "
         CR ." 1^1 + 2^2 + 3^3 + ... + 1000^1000." ;

        .ABOUT

\ FORTH> euler48
\ The last ten digits of the series 1^1 + 2^2 + 3^3 + ... + 1000^1000 are xxxxxxxxxx
\ 0.011 seconds elapsed. ok


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bernd Paysan  
View profile
 More options May 8, 5:22 am
Newsgroups: comp.lang.forth
From: Bernd Paysan <bernd.pay...@gmx.de>
Date: Thu, 08 May 2008 11:22:15 +0200
Local: Thurs, May 8 2008 5:22 am
Subject: Re: Euler problem #48

Marcel Hendrix wrote:
> INCLUDE ../bignum/factor.frt

If you have a 64 bit Forth (and I know you have ;-), you don't need bignums
for that. All intermediate results have at most 20 digits, and therefore
easily fit in a 128 bit number; the sum with at most 13 digits fits in a 64
bit number. This one should be faster (how much depends on how efficient
your gs^mod actually is). In Gforth with a 2GHz Athlon64, it takes about
1.5ms to run 1000t. I guess iForth64 should do it in less than 1ms, even
when the division for the modulus might be the limiting factor.

\ sum of the last 10 digits of n^n for n=1..1000

Cell 8 < [IF] .( Needs a 64 bit Forth!) cr true abort  [THEN]

&10000000000 Constant mod#

: *mod ( a b -- c )  um* mod# um/mod drop ;

: **mod ( x n -- n )  >r 1 swap
    BEGIN  r@ 1 and  IF  tuck *mod swap  THEN
        r> 2/ dup  WHILE  >r dup *mod  REPEAT
    2drop ;

: e48 ( n -- g )
    0 swap 1+ 1 DO
        I dup **mod +
    LOOP  mod# mod ;

: Euler48 ( -- )  
         CR ." The last ten digits of the series "
         ." 1^1 + 2^2 + 3^3 + ... + 1000^1000 are " 1000 e48 . cr ;

--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://www.jwdt.com/~paysan/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Anton Ertl  
View profile
 More options May 8, 6:28 am
Newsgroups: comp.lang.forth
From: an...@mips.complang.tuwien.ac.at (Anton Ertl)
Date: Thu, 08 May 2008 10:28:32 GMT
Local: Thurs, May 8 2008 6:28 am
Subject: Re: Euler problem #48

Bernd Paysan <bernd.pay...@gmx.de> writes:
>In Gforth with a 2GHz Athlon64, it takes about
>1.5ms to run 1000t. I guess iForth64 should do it in less than 1ms, even
>when the division for the modulus might be the limiting factor.

If you timed it with the development version of gforth-fast, I doubt
it.  I guesstimate that the program spends more then 1ms on UM/MOD on
your machine.

>&10000000000 Constant mod#

>: *mod ( a b -- c )  um* mod# um/mod drop ;

Wasn't there a way to do integer division (and consequently compute
the modulus) by multiplication with the inverse?

- anton
--
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2008: http://www.complang.tuwien.ac.at/anton/euroforth/ef08.html


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Pornin  
View profile
 More options May 8, 10:18 am
Newsgroups: comp.lang.forth
From: Thomas Pornin <por...@bolet.org>
Date: 08 May 2008 14:18:40 GMT
Local: Thurs, May 8 2008 10:18 am
Subject: Re: Euler problem #48
According to Anton Ertl <an...@mips.complang.tuwien.ac.at>:

> Wasn't there a way to do integer division (and consequently compute
> the modulus) by multiplication with the inverse?

There is an article by Granlund and Montgomery on that subject:
"Division By Invariant Integers using Multiplication". It can be
found there:

   http://citeseer.ist.psu.edu/granlund94division.html

        --Thomas Pornin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bernd Paysan  
View profile
 More options May 8, 10:33 am
Newsgroups: comp.lang.forth
From: Bernd Paysan <bernd.pay...@gmx.de>
Date: Thu, 08 May 2008 16:33:41 +0200
Local: Thurs, May 8 2008 10:33 am
Subject: Re: Euler problem #48

Anton Ertl wrote:
> Bernd Paysan <bernd.pay...@gmx.de> writes:
>>In Gforth with a 2GHz Athlon64, it takes about
>>1.5ms to run 1000t. I guess iForth64 should do it in less than 1ms, even
>>when the division for the modulus might be the limiting factor.

> If you timed it with the development version of gforth-fast, I doubt
> it.  I guesstimate that the program spends more then 1ms on UM/MOD on
> your machine.

I had it run with the normal gforth. Gforth-fast on the 2GHz Athlon64:
955µs, on a 2.6GHz Core 2 Quad 650µs. So actually iForth64 must be under
one 1ms, too ;-).

>>&10000000000 Constant mod#

>>: *mod ( a b -- c )  um* mod# um/mod drop ;

> Wasn't there a way to do integer division (and consequently compute
> the modulus) by multiplication with the inverse?

Won't help much, since we need to divide a 128 bit number, and that requires
too many multiplications to get the result (the inverse also must be a 128
bit number to be accurate enough, so we have four multiplications to divide
down, and then another multiplication to restore the modulus from the
fractional part). It's probably possible to use a 64 bit number and the
right amount of bitshift, but for a Euler problem solution, this is too
much work ;-).

I've tried something else: Using fractional numbers, i.e. scaling by 1e-10
and having the decimal point on bit 64. The problem is the same: If you use
just a 64 bit inverse of 1e10, you don't have much accuracy left. Using 128
bits requires too many multiplications, and I still didn't get it accurate
enough.

--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://www.jwdt.com/~paysan/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile
 More options May 8, 1:00 pm
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 08 May 2008 17:00:16 GMT
Local: Thurs, May 8 2008 1:00 pm
Subject: Re: Euler problem #48

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

Groetjes Albert

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile
 More options May 8, 10:08 pm
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 09 May 2008 02:08:35 GMT
Local: Thurs, May 8 2008 10:08 pm
Subject: Re: Euler problem #48
In article <5jfbf5-14d....@annette.mikron.de>,
Bernd Paysan  <bernd.pay...@gmx.de> wrote:
<SNIP>

>I've tried something else: Using fractional numbers, i.e. scaling by 1e-10
>and having the decimal point on bit 64. The problem is the same: If you use
>just a 64 bit inverse of 1e10, you don't have much accuracy left. Using 128
>bits requires too many multiplications, and I still didn't get it accurate
>enough.

This too must work:
If we are prepared to do N^2 operations instead of N log N, for an
exponentiation, we can calculate 877^877 without needing more
precision than double on a 32 bit Forth.
Repeatedly multiply with 877. This remains under 1000*10^10.
Then calculate the last 10 digits by first calculating the
last 5, then the last 5 of the remainder. (Using FM/MOD)
Combine, rinse, repeat.
(OK this is a very limited form of multiple precision.)

(Oeps, we are now into 1000^3 operations. This may become
an overnighty.)

Some of the problems give me the idea I'm cheating when using
a 64 bit Forth, in the sense that you don't need to address the
real challenge.

>--
>Bernd Paysan
>"If you want it done right, you have to do it yourself"
>http://www.jwdt.com/~paysan/

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile
 More options May 9, 11:44 am
Newsgroups: comp.lang.forth
From: William James <w_a_x_...@yahoo.com>
Date: Fri, 9 May 2008 08:44:42 -0700 (PDT)
Local: Fri, May 9 2008 11:44 am
Subject: Re: Euler problem #48
On May 7, 3:55 pm, m...@iae.nl (Marcel Hendrix) wrote:

In PARI/gp.  0 ms.  It yields more than 10 digits.

sum(i=1,1000,lift( Mod(i,10^10)^i ) )


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marcel Hendrix  
View profile
 More options May 9, 1:13 pm
Newsgroups: comp.lang.forth
From: m...@iae.nl (Marcel Hendrix)
Date: Fri, 9 May 2008 19:13:05 +0200
Local: Fri, May 9 2008 1:13 pm
Subject: [SPOILER] Re: Euler problem #48
William James <w_a_x_...@yahoo.com> wrote Re: Euler problem #48

>> \ FORTH> euler48
>> \ The last ten digits of the series 1^1 + 2^2 + 3^3 + ... + 1000^1000 are xxxxxxxxxx
>> \ 0.011 seconds elapsed. ok
> In PARI/gp.  0 ms.  It yields more than 10 digits.
> sum(i=1,1000,lift( Mod(i,10^10)^i ) )

You mean, it calculates the wrong answer, faster?

Sum = Mod( sum(i=1,1000,lift( Mod(i,10^10)^i ) ), 10^10 )

Fixes that, surely.

-marcel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile
 More options May 9, 6:07 pm
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 09 May 2008 22:07:32 GMT
Local: Fri, May 9 2008 6:07 pm
Subject: Re: [SPOILER] Re: Euler problem #48

In article <18103614183...@frunobulax.edu>, Marcel Hendrix <m...@iae.nl> wrote:
>William James <w_a_x_...@yahoo.com> wrote Re: Euler problem #48

>>> \ FORTH> euler48
>>> \ The last ten digits of the series 1^1 + 2^2 + 3^3 + ... + 1000^1000 are xxxxxxxxxx
>>> \ 0.011 seconds elapsed. ok

>> In PARI/gp.  0 ms.  It yields more than 10 digits.

>> sum(i=1,1000,lift( Mod(i,10^10)^i ) )

>You mean, it calculates the wrong answer, faster?

>Sum = Mod( sum(i=1,1000,lift( Mod(i,10^10)^i ) ), 10^10 )

Methink that for i<1001   Mod(i,10^10) always equals i.
What do I miss here?

>Fixes that, surely.

>-marcel

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google