Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #58877 > unrolled thread
| Started by | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| First post | 2017-05-09 08:29 +0000 |
| Last post | 2017-05-18 09:29 +0000 |
| Articles | 19 — 8 participants |
Back to article view | Back to comp.lang.forth
Rosetta: Babbage problem "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-09 08:29 +0000
Re: Rosetta: Babbage problem mhx@iae.nl - 2017-05-09 12:58 -0700
Re: Rosetta: Babbage problem Paul Rubin <no.email@nospam.invalid> - 2017-05-09 20:43 -0700
Re: Rosetta: Babbage problem Julian Fondren <julian.fondren@gmail.com> - 2017-05-09 23:43 -0700
Re: Rosetta: Babbage problem Lars Brinkhoff <lars.spam@nocrew.org> - 2017-05-10 09:06 +0200
Re: Rosetta: Babbage problem Julian Fondren <julian.fondren@gmail.com> - 2017-05-10 00:27 -0700
Re: Rosetta: Babbage problem Lars Brinkhoff <lars.spam@nocrew.org> - 2017-05-10 09:32 +0200
Re: Rosetta: Babbage problem Paul Rubin <no.email@nospam.invalid> - 2017-05-10 00:59 -0700
Re: Rosetta: Babbage problem Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-05-10 08:14 -0500
Re: Rosetta: Babbage problem mhx@iae.nl - 2017-05-10 02:59 -0700
Re: Rosetta: Babbage problem Bruno Gauthier <bgauthier@free.fr> - 2017-05-15 12:25 +0200
Re: Rosetta: Babbage problem mhx@iae.nl - 2017-05-15 04:17 -0700
Re: Rosetta: Babbage problem Bruno Gauthier <bgauthier@free.fr> - 2017-05-15 13:48 +0200
Re: Rosetta: Babbage problem Julian Fondren <julian.fondren@gmail.com> - 2017-05-15 04:56 -0700
Re: Rosetta: Babbage problem Bruno Gauthier <bgauthier@free.fr> - 2017-05-15 16:33 +0200
Re: Rosetta: Babbage problem mhx@iae.nl - 2017-05-15 10:53 -0700
Re: Rosetta: Babbage problem albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-05-15 21:39 +0200
Re: Rosetta: Babbage problem "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-18 09:23 +0000
Re: Rosetta: Babbage problem "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-18 09:29 +0000
| From | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| Date | 2017-05-09 08:29 +0000 |
| Subject | Rosetta: Babbage problem |
| Message-ID | <oerulu$1tuq$1@gioia.aioe.org> |
> Charles Babbage, looking ahead to the sorts of problems his > Analytical Engine would be able to solve, gave this example: > > What is the smallest positive integer whose square ends in the > digits 269,696? -- Babbage, letter to Lord Bowden, 1837; see > Hollingdale and Tootill, Electronic Computers, second edition, > 1970, p. 125. > > He thought the answer might be 99,736, whose square is > 9,947,269,696; but he couldn't be certain. > > Task > > The task is to find out if Babbage had the right answer -- and > to do so, as far as your language allows it, in code that > Babbage himself would have been able to read and understand. > > For these purposes, Charles Babbage may be taken to be an > intelligent person, familiar with mathematics and with the > idea of a computer, who has never programmed--in fact, who has > never so much as seen a single line of code. > > Motivation > > The aim of the task is to write a program that is sufficiently > clear and well-documented for such a person to be able to read > it and be confident that it does indeed solve the specified > problem. (use srfi-121) ;; Create function that accepts a number and returns ;; true if, only if, it satisfies the requirements. (define (good? n) (equal? 269696 (modulo (* n n) 1000000))) ;; A generator for the natural numbers (1,2,3,4 ...). ;; Each time it is called, it yields the next number. (define generate-naturals (make-range-generator 1)) (generator-find good? generate-naturals) ===> 25264 ----- (use lazy-lists) (define (good? n) (equal? 269696 (modulo (* n n) 1000000))) (First (Filter good? (Cardinals))) ===> 25264 In Forth? -- [A]n unholy alliance of leftists, capitalists, and Zionist supremacists has schemed to promote immigration and miscegenation with the deliberate aim of breeding us out of existence in our own homelands.... [T]he real aim stays the same: the biggest genocide in human history.... --- Nick Griffin (https://www.youtube.com/watch?v=K0hD7IffTJs)
[toc] | [next] | [standalone]
| From | mhx@iae.nl |
|---|---|
| Date | 2017-05-09 12:58 -0700 |
| Message-ID | <ab981ee5-550e-4be0-ad5d-39471ce05e2e@googlegroups.com> |
| In reply to | #58877 |
On Tuesday, May 9, 2017 at 10:29:55 AM UTC+2, Robert L. wrote:
> > Charles Babbage, looking ahead to the sorts of problems his
> > Analytical Engine would be able to solve, gave this example:
> >
> > What is the smallest positive integer whose square ends in the
> > digits 269,696? -- Babbage, letter to Lord Bowden, 1837; see
> > Hollingdale and Tootill, Electronic Computers, second edition,
> > 1970, p. 125.
> >
> > He thought the answer might be 99,736, whose square is
> > 9,947,269,696; but he couldn't be certain.
> >
> > Task
> >
> > The task is to find out if Babbage had the right answer -- and
> > to do so, as far as your language allows it, in code that
> > Babbage himself would have been able to read and understand.
> >
> > For these purposes, Charles Babbage may be taken to be an
> > intelligent person, familiar with mathematics and with the
> > idea of a computer, who has never programmed--in fact, who has
> > never so much as seen a single line of code.
> >
> > Motivation
> >
> > The aim of the task is to write a program that is sufficiently
> > clear and well-documented for such a person to be able to read
> > it and be confident that it does indeed solve the specified
> > problem.
Nice, but this is *really* not much of a challenge.
FORTH> : babbage ( -- ) \ use all provided knowledge
#99736 #519 do I I * #269696 - #1000000 mod 0=
IF I . unloop exit ENDIF
loop cr ." not found!" ; ok
FORTH> babbage 25264 ok
-marcel
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2017-05-09 20:43 -0700 |
| Message-ID | <87mvall6l5.fsf@nightsong.com> |
| In reply to | #58911 |
mhx@iae.nl writes: > FORTH> : babbage ( -- ) \ use all provided knowledge > #99736 #519 do I I * #269696 - #1000000 mod 0= ... : check ( n -- flag ) dup * 1000000 mod 269696 = ; : go ( -- ) 0 begin 1+ dup check if . exit then again ; go 25264 ok
[toc] | [prev] | [next] | [standalone]
| From | Julian Fondren <julian.fondren@gmail.com> |
|---|---|
| Date | 2017-05-09 23:43 -0700 |
| Message-ID | <799db5cd-cad1-4fd3-999d-e0effce1fd80@googlegroups.com> |
| In reply to | #58918 |
On Tuesday, May 9, 2017 at 10:43:37 PM UTC-5, Paul Rubin wrote: > mhx@iae.nl writes: > > FORTH> : babbage ( -- ) \ use all provided knowledge > > #99736 #519 do I I * #269696 - #1000000 mod 0= ... > > : check ( n -- flag ) dup * 1000000 mod 269696 = ; > : go ( -- ) 0 begin 1+ dup check if . exit then again ; > > go 25264 ok : go ( -- ) 0 begin 1+ dup check until . ; ok go 25264 ok :)
[toc] | [prev] | [next] | [standalone]
| From | Lars Brinkhoff <lars.spam@nocrew.org> |
|---|---|
| Date | 2017-05-10 09:06 +0200 |
| Message-ID | <864lwtb38n.fsf@molnjunk.nocrew.org> |
| In reply to | #58924 |
Julian Fondren wrote: > Paul Rubin wrote: >> : check ( n -- flag ) dup * 1000000 mod 269696 = ; >> : go ( -- ) 0 begin 1+ dup check if . exit then again ; > > : go ( -- ) 0 begin 1+ dup check until . ; ok I believe Thinking Forth advises to not pass around boolean flags, but to act on the information as soon as possible. In that vein: : check ( n -- ) dup * 1000000 mod 269696 = if . quit then ; : go 0 begin 1+ dup check again ;
[toc] | [prev] | [next] | [standalone]
| From | Julian Fondren <julian.fondren@gmail.com> |
|---|---|
| Date | 2017-05-10 00:27 -0700 |
| Message-ID | <99838afb-d3ec-41e5-b273-f2ae5f130506@googlegroups.com> |
| In reply to | #58926 |
On Wednesday, May 10, 2017 at 2:06:02 AM UTC-5, Lars Brinkhoff wrote: > Julian Fondren wrote: > > Paul Rubin wrote: > >> : check ( n -- flag ) dup * 1000000 mod 269696 = ; > >> : go ( -- ) 0 begin 1+ dup check if . exit then again ; > > > > : go ( -- ) 0 begin 1+ dup check until . ; ok > > I believe Thinking Forth advises to not pass around boolean flags, but > to act on the information as soon as possible. In that vein: : go ( -- ) 0 begin 1+ dup dup * 1000000 mod 269696 = until . ; Now you're 'acting on the information as soon as possible'... and it's the exact same code as mine. Can serious design flaws go away just from the inlining of a factor? > > : check ( n -- ) dup * 1000000 mod 269696 = if . quit then ; > : go 0 begin 1+ dup check again ; I like it that a misapplication of a rule results in clearly inferior code :-) CHECK wasn't 'passing around boolean flags'; it just factored out the check. As the name suggests. A good auxiliary rule is probably that a correction of 'passing around boolean flags' should result in fewer overall conditionals. Because checking a flag and then *not acting on it* but instead *producing another flag* is how you get the excess of conditionals.
[toc] | [prev] | [next] | [standalone]
| From | Lars Brinkhoff <lars.spam@nocrew.org> |
|---|---|
| Date | 2017-05-10 09:32 +0200 |
| Message-ID | <86ziel9nff.fsf@molnjunk.nocrew.org> |
| In reply to | #58933 |
Julian Fondren wrote: > CHECK wasn't 'passing around boolean flags'; it just factored out the > check. As the name suggests. A good auxiliary rule is probably that a > correction of 'passing around boolean flags' should result in fewer > overall conditionals. Because checking a flag and then *not acting on > it* but instead *producing another flag* is how you get the excess of > conditionals. Yes, you're right!
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2017-05-10 00:59 -0700 |
| Message-ID | <87pofhb0qq.fsf@nightsong.com> |
| In reply to | #58926 |
Lars Brinkhoff <lars.spam@nocrew.org> writes: > I believe Thinking Forth advises to not pass around boolean flags Oh that's interesting, I hadn't known about that. But don't all the usual comparison words like < return flags? I wonder what the problem is. > : go 0 begin 1+ dup check again ; I still like Julian's version better. I'd forgotten about UNTIL.
[toc] | [prev] | [next] | [standalone]
| From | Andrew Haley <andrew29@littlepinkcloud.invalid> |
|---|---|
| Date | 2017-05-10 08:14 -0500 |
| Message-ID | <RaSdnbjAAfWGj47EnZ2dnUU7-VWdnZ2d@supernews.com> |
| In reply to | #58941 |
Paul Rubin <no.email@nospam.invalid> wrote: > Lars Brinkhoff <lars.spam@nocrew.org> writes: >> I believe Thinking Forth advises to not pass around boolean flags > > Oh that's interesting, I hadn't known about that. But don't all the > usual comparison words like < return flags? I wonder what the problem > is. The idea is that once you know X, you act on it. And you refactor as needed to make that possible. It doesn't always work and it's not a perfect heuristic, but it's useful to try it if you find yourself saying IF more than once on the same condition. Errors are a classic case, where every word ends up returning a flag, and every caller ends up testing it. Andrew.
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl |
|---|---|
| Date | 2017-05-10 02:59 -0700 |
| Message-ID | <a03bcd45-825f-4646-9cc6-5f28df5c915e@googlegroups.com> |
| In reply to | #58918 |
On Wednesday, May 10, 2017 at 5:43:37 AM UTC+2, Paul Rubin wrote: > mhx@iae.nl writes: > > FORTH> : babbage ( -- ) \ use all provided knowledge > > #99736 #519 do I I * #269696 - #1000000 mod 0= ... > > : check ( n -- flag ) dup * 1000000 mod 269696 = ; > : go ( -- ) 0 begin 1+ dup check if . exit then again ; > > go 25264 ok That is much better :-) To my defense, I typed the code without planning on a single line and it worked first time (to my astonishment). Mission accomplished. -marcel
[toc] | [prev] | [next] | [standalone]
| From | Bruno Gauthier <bgauthier@free.fr> |
|---|---|
| Date | 2017-05-15 12:25 +0200 |
| Message-ID | <591981d3$0$3709$426a74cc@news.free.fr> |
| In reply to | #58953 |
Le 10/05/2017 à 11:59, mhx@iae.nl a écrit : > On Wednesday, May 10, 2017 at 5:43:37 AM UTC+2, Paul Rubin wrote: >> mhx@iae.nl writes: >>> FORTH> : babbage ( -- ) \ use all provided knowledge >>> #99736 #519 do I I * #269696 - #1000000 mod 0= ... >> >> : check ( n -- flag ) dup * 1000000 mod 269696 = ; >> : go ( -- ) 0 begin 1+ dup check if . exit then again ; >> >> go 25264 ok > > That is much better :-) > > To my defense, I typed the code without planning > on a single line and it worked first time (to my astonishment). > Mission accomplished. > > -marcel > another way with less iterations : check dup s>f fsqrt f>s dup dup * rot = ; : go 1000000 269696 begin over + dup check dup if swap . nip then nip until ; bruno
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl |
|---|---|
| Date | 2017-05-15 04:17 -0700 |
| Message-ID | <bd7ddab1-3eeb-4d07-b8e1-771370ec3abb@googlegroups.com> |
| In reply to | #59287 |
On Monday, May 15, 2017 at 12:24:20 PM UTC+2, Bruno Gauthier wrote: > Le 10/05/2017 à 11:59, mhx@iae.nl a écrit : > > On Wednesday, May 10, 2017 at 5:43:37 AM UTC+2, Paul Rubin wrote: > >> mhx@iae.nl writes: > >>> FORTH> : babbage ( -- ) \ use all provided knowledge > >>> #99736 #519 do I I * #269696 - #1000000 mod 0= ... > >> > >> : check ( n -- flag ) dup * 1000000 mod 269696 = ; > >> : go ( -- ) 0 begin 1+ dup check if . exit then again ; > >> > >> go 25264 ok > > > > That is much better :-) > > > > To my defense, I typed the code without planning > > on a single line and it worked first time (to my astonishment). > > Mission accomplished. > > > > -marcel > > > another way with less iterations > : check dup s>f fsqrt f>s dup dup * rot = ; > : go 1000000 269696 begin over + dup check dup if swap . nip then nip > until ; > > bruno Eight times more iterations, an fsqrt in the loop, and worries about rounding? -marcel
[toc] | [prev] | [next] | [standalone]
| From | Bruno Gauthier <bgauthier@free.fr> |
|---|---|
| Date | 2017-05-15 13:48 +0200 |
| Message-ID | <59199523$0$4818$426a74cc@news.free.fr> |
| In reply to | #59292 |
Le 15/05/2017 à 13:17, mhx@iae.nl a écrit : > On Monday, May 15, 2017 at 12:24:20 PM UTC+2, Bruno Gauthier wrote: >> Le 10/05/2017 à 11:59, mhx@iae.nl a écrit : >>> On Wednesday, May 10, 2017 at 5:43:37 AM UTC+2, Paul Rubin wrote: >>>> mhx@iae.nl writes: >>>>> FORTH> : babbage ( -- ) \ use all provided knowledge >>>>> #99736 #519 do I I * #269696 - #1000000 mod 0= ... >>>> >>>> : check ( n -- flag ) dup * 1000000 mod 269696 = ; >>>> : go ( -- ) 0 begin 1+ dup check if . exit then again ; >>>> >>>> go 25264 ok >>> >>> That is much better :-) >>> >>> To my defense, I typed the code without planning >>> on a single line and it worked first time (to my astonishment). >>> Mission accomplished. >>> >>> -marcel >>> >> another way with less iterations >> : check dup s>f fsqrt f>s dup dup * rot = ; >> : go 1000000 269696 begin over + dup check dup if swap . nip then nip >> until ; >> >> bruno > > Eight times more iterations, an fsqrt in the loop, and worries about rounding? > > -marcel > no, only 638 iterations, only the numbers with last digits with 269696 are checked. it start at 1269696 and finish at 63869696. but ok i use fsqrt and i dont think about rounding there perhaps i should. 0 value iterations : check 1 +to iterations dup s>f fsqrt f>s dup dup * rot = ; : go 1000000 269696 begin over + dup check dup if swap . nip then nip until cr ." Iterations : " iterations . ;
[toc] | [prev] | [next] | [standalone]
| From | Julian Fondren <julian.fondren@gmail.com> |
|---|---|
| Date | 2017-05-15 04:56 -0700 |
| Message-ID | <9211975a-debe-4366-afdf-216ae6573b0e@googlegroups.com> |
| In reply to | #59296 |
On Monday, May 15, 2017 at 6:46:44 AM UTC-5, Bruno Gauthier wrote:
> Le 15/05/2017 à 13:17, mhx@iae.nl a écrit :
> > On Monday, May 15, 2017 at 12:24:20 PM UTC+2, Bruno Gauthier wrote:
> >> Le 10/05/2017 à 11:59, mhx@iae.nl a écrit :
> >> another way with less iterations
> >> : check dup s>f fsqrt f>s dup dup * rot = ;
> >> : go 1000000 269696 begin over + dup check dup if swap . nip then nip
> >> until ;
> >>
> >> bruno
> >
> > Eight times more iterations, an fsqrt in the loop, and worries about rounding?
> >
> > -marcel
> >
> no, only 638 iterations, only the numbers with last digits with 269696
> are checked. it start at 1269696 and finish at 63869696.
> but ok i use fsqrt and i dont think about rounding there perhaps i should.
>
>
> 0 value iterations
>
> : check 1 +to iterations dup s>f fsqrt f>s dup dup * rot = ;
> : go 1000000 269696 begin over + dup check dup if swap . nip then nip until
> cr ." Iterations : " iterations .
> ;
variable answer
: . ( x -- ) answer ! ;
: check ( n -- flag ) dup * 1000000 mod 269696 = ;
: go1 ( -- ) 0 begin 1+ dup check until . ;
: check dup s>f fsqrt f>s dup dup * rot = ;
: go2 1000000 269696 begin over + dup check dup if swap . nip then nip
until ;
: bench ( n -- )
cr
cr ." go1: " timer-reset dup 0 do go1 loop .elapsed
cr ." go2: " timer-reset 0 do go2 loop .elapsed ;
5000 runs on my machine:
go1: 1.128 seconds elapsed.
go2: 0.014 seconds elapsed.
tsk. What's with this micro-optimization? :-)
[toc] | [prev] | [next] | [standalone]
| From | Bruno Gauthier <bgauthier@free.fr> |
|---|---|
| Date | 2017-05-15 16:33 +0200 |
| Message-ID | <5919bbce$0$3631$426a74cc@news.free.fr> |
| In reply to | #59298 |
Le 15/05/2017 à 13:56, Julian Fondren a écrit : > On Monday, May 15, 2017 at 6:46:44 AM UTC-5, Bruno Gauthier wrote: >> Le 15/05/2017 à 13:17, mhx@iae.nl a écrit : >>> On Monday, May 15, 2017 at 12:24:20 PM UTC+2, Bruno Gauthier wrote: >>>> Le 10/05/2017 à 11:59, mhx@iae.nl a écrit : >>>> another way with less iterations >>>> : check dup s>f fsqrt f>s dup dup * rot = ; >>>> : go 1000000 269696 begin over + dup check dup if swap . nip then nip >>>> until ; >>>> >>>> bruno >>> >>> Eight times more iterations, an fsqrt in the loop, and worries about rounding? >>> >>> -marcel >>> >> no, only 638 iterations, only the numbers with last digits with 269696 >> are checked. it start at 1269696 and finish at 63869696. >> but ok i use fsqrt and i dont think about rounding there perhaps i should. >> >> >> 0 value iterations >> >> : check 1 +to iterations dup s>f fsqrt f>s dup dup * rot = ; >> : go 1000000 269696 begin over + dup check dup if swap . nip then nip until >> cr ." Iterations : " iterations . >> ; > > variable answer > : . ( x -- ) answer ! ; > > : check ( n -- flag ) dup * 1000000 mod 269696 = ; > : go1 ( -- ) 0 begin 1+ dup check until . ; > > : check dup s>f fsqrt f>s dup dup * rot = ; > : go2 1000000 269696 begin over + dup check dup if swap . nip then nip > until ; > > : bench ( n -- ) > cr > cr ." go1: " timer-reset dup 0 do go1 loop .elapsed > cr ." go2: " timer-reset 0 do go2 loop .elapsed ; > > 5000 runs on my machine: > > go1: 1.128 seconds elapsed. > go2: 0.014 seconds elapsed. > > tsk. What's with this micro-optimization? :-) > marcel soluce started at 519 not 0, you are not fair lol to be serious, paul soluce is elegante and marcel soluce already an optimization but he was the first to give the soluce. so respect
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl |
|---|---|
| Date | 2017-05-15 10:53 -0700 |
| Message-ID | <94210460-0d3f-428f-85d0-9621a94ad82a@googlegroups.com> |
| In reply to | #59296 |
On Monday, May 15, 2017 at 1:46:44 PM UTC+2, Bruno Gauthier wrote: > Le 15/05/2017 à 13:17, mhx@iae.nl a écrit : > > On Monday, May 15, 2017 at 12:24:20 PM UTC+2, Bruno Gauthier wrote: > >> Le 10/05/2017 à 11:59, mhx@iae.nl a écrit : > >>> On Wednesday, May 10, 2017 at 5:43:37 AM UTC+2, Paul Rubin wrote: > >>>> mhx@iae.nl writes: > >>>>> FORTH> : babbage ( -- ) \ use all provided knowledge > >>>>> #99736 #519 do I I * #269696 - #1000000 mod 0= ... > >>>> > >>>> : check ( n -- flag ) dup * 1000000 mod 269696 = ; > >>>> : go ( -- ) 0 begin 1+ dup check if . exit then again ; > >>>> > >>>> go 25264 ok > >>> > >>> That is much better :-) > >>> > >>> To my defense, I typed the code without planning > >>> on a single line and it worked first time (to my astonishment). > >>> Mission accomplished. > >>> > >>> -marcel > >>> > >> another way with less iterations > >> : check dup s>f fsqrt f>s dup dup * rot = ; > >> : go 1000000 269696 begin over + dup check dup if swap . nip then nip > >> until ; > >> > >> bruno > > > > Eight times more iterations, an fsqrt in the loop, and worries about rounding? > > > > -marcel > > > no, only 638 iterations, only the numbers with last digits with 269696 > are checked. it start at 1269696 and finish at 63869696. > but ok i use fsqrt and i dont think about rounding there perhaps i should. > > > 0 value iterations > > : check 1 +to iterations dup s>f fsqrt f>s dup dup * rot = ; > : go 1000000 269696 begin over + dup check dup if swap . nip then nip until > cr ." Iterations : " iterations . > ; Uh oh. Sorry Bruno! Only when Julian benched your code I noticed the *very elegant algorithmic trick*. I couldn't read your code without pen and paper and assumed you were simply incrementing by 1. Next time, you might consider using a few stack comments :-) I wonder why Babbage didn't see your trick. Maybe the necessary square root? -marcel
[toc] | [prev] | [next] | [standalone]
| From | albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) |
|---|---|
| Date | 2017-05-15 21:39 +0200 |
| Message-ID | <ofd05m$agr$1@cherry.spenarnc.xs4all.nl> |
| In reply to | #59296 |
In article <59199523$0$4818$426a74cc@news.free.fr>, Bruno Gauthier <bgauthier@free.fr> wrote: >Le 15/05/2017 à 13:17, mhx@iae.nl a écrit : >> On Monday, May 15, 2017 at 12:24:20 PM UTC+2, Bruno Gauthier wrote: >>> Le 10/05/2017 à 11:59, mhx@iae.nl a écrit : >>>> On Wednesday, May 10, 2017 at 5:43:37 AM UTC+2, Paul Rubin wrote: >>>>> mhx@iae.nl writes: >>>>>> FORTH> : babbage ( -- ) \ use all provided knowledge >>>>>> #99736 #519 do I I * #269696 - #1000000 mod 0= ... >>>>> >>>>> : check ( n -- flag ) dup * 1000000 mod 269696 = ; >>>>> : go ( -- ) 0 begin 1+ dup check if . exit then again ; >>>>> >>>>> go 25264 ok >>>> >>>> That is much better :-) >>>> >>>> To my defense, I typed the code without planning >>>> on a single line and it worked first time (to my astonishment). >>>> Mission accomplished. >>>> >>>> -marcel >>>> >>> another way with less iterations >>> : check dup s>f fsqrt f>s dup dup * rot = ; >>> : go 1000000 269696 begin over + dup check dup if swap . nip then nip >>> until ; >>> >>> bruno >> >> Eight times more iterations, an fsqrt in the loop, and worries about rounding? >> >> -marcel >> >no, only 638 iterations, only the numbers with last digits with 269696 >are checked. it start at 1269696 and finish at 63869696. >but ok i use fsqrt and i dont think about rounding there perhaps i should. ciforth : WANT SQ? : doit 269696 BEGIN 1,000,000 + DUP SQ? UNTIL ; doit . 638269696 OK The joy of a source library based on the name of words. (In a NEEDS or REQUIRE system, one wouldn't think of putting SQ? in the library, and if one did, one couldn't find it.) Groetjes Albert -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
[toc] | [prev] | [next] | [standalone]
| From | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| Date | 2017-05-18 09:23 +0000 |
| Message-ID | <ofjp60$1tv2$1@gioia.aioe.org> |
| In reply to | #58877 |
On 5/9/2017, Robert L. wrote: > > Charles Babbage, looking ahead to the sorts of problems his > > Analytical Engine would be able to solve, gave this example: > > > > What is the smallest positive integer whose square ends in the > > digits 269,696? -- Babbage, letter to Lord Bowden, 1837; see > > Hollingdale and Tootill, Electronic Computers, second edition, > > 1970, p. 125. > > > > He thought the answer might be 99,736, whose square is > > 9,947,269,696; but he couldn't be certain. > > > > Task > > > > The task is to find out if Babbage had the right answer -- and > > to do so, as far as your language allows it, in code that > > Babbage himself would have been able to read and understand. > > > > For these purposes, Charles Babbage may be taken to be an > > intelligent person, familiar with mathematics and with the > > idea of a computer, who has never programmed--in fact, who has > > never so much as seen a single line of code. > > > > Motivation > > > > The aim of the task is to write a program that is sufficiently > > clear and well-documented for such a person to be able to read > > it and be confident that it does indeed solve the specified > > problem. > > > (use srfi-121) > > ;; Create function that accepts a number and returns > ;; true if, only if, it satisfies the requirements. > (define (good? n) (equal? 269696 (modulo (* n n) 1000000))) > > ;; A generator for the natural numbers (1,2,3,4 ...). > ;; Each time it is called, it yields the next number. > (define generate-naturals (make-range-generator 1)) > > (generator-find good? generate-naturals) > > ===> > 25264 > > ----- > > (use lazy-lists) > > (define (good? n) (equal? 269696 (modulo (* n n) 1000000))) > > (First (Filter good? (Cardinals))) > > ===> > 25264 > > > In Forth? (let go ((i 0)) (if (= 269696 (modulo (* i i) 1000000)) i (go (+ 1 i)))) ===> 25264 -- [H]e was prosecuted ... for "denying" the gas chambers and the six million figure.... [A] Swiss court sentenced him to 15 months imprisonment.... Rather than serve the sentence, ... Graf went into exile. In 2001 he married a Russian historian in Moscow. www.revisionists.com/revisionists/graf.html
[toc] | [prev] | [next] | [standalone]
| From | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| Date | 2017-05-18 09:29 +0000 |
| Message-ID | <ofjpi3$1uk7$1@gioia.aioe.org> |
| In reply to | #59477 |
On 5/18/2017, Robert L. wrote: > On 5/9/2017, Robert L. wrote: > > > > Charles Babbage, looking ahead to the sorts of problems his > > > Analytical Engine would be able to solve, gave this example: > > > > > > What is the smallest positive integer whose square ends in the > > > digits 269,696? -- Babbage, letter to Lord Bowden, 1837; see > > > Hollingdale and Tootill, Electronic Computers, second edition, > > > 1970, p. 125. > > > > > > He thought the answer might be 99,736, whose square is > > > 9,947,269,696; but he couldn't be certain. > > > > > > Task > > > > > > The task is to find out if Babbage had the right answer -- and > > > to do so, as far as your language allows it, in code that > > > Babbage himself would have been able to read and understand. > > > > > > For these purposes, Charles Babbage may be taken to be an > > > intelligent person, familiar with mathematics and with the > > > idea of a computer, who has never programmed--in fact, who has > > > never so much as seen a single line of code. > > > > > > Motivation > > > > > > The aim of the task is to write a program that is sufficiently > > > clear and well-documented for such a person to be able to read > > > it and be confident that it does indeed solve the specified > > > problem. > > > > > > (use srfi-121) > > > > ;; Create function that accepts a number and returns > > ;; true if, only if, it satisfies the requirements. > > (define (good? n) (equal? 269696 (modulo (* n n) 1000000))) > > > > ;; A generator for the natural numbers (1,2,3,4 ...). > > ;; Each time it is called, it yields the next number. > > (define generate-naturals (make-range-generator 1)) > > > > (generator-find good? generate-naturals) > > > > ===> > > 25264 > > > > ----- > > > > (use lazy-lists) > > > > (define (good? n) (equal? 269696 (modulo (* n n) 1000000))) > > > > (First (Filter good? (Cardinals))) > > > > ===> > > 25264 > > > > > > In Forth? > > (let go ((i 0)) (if (= 269696 (modulo (* i i) 1000000)) i (go (+ 1 i)))) > ===> > 25264 (do ((i 0 (+ 1 i))) ((= 269696 (modulo (* i i) 1000000)) i)) ===> 25264 -- For two years ... he was held in solitary confinement in the Toronto West Detention Centre.... [A] court in Mannheim sentenced him to five years imprisonment for the crime of "popular incitement" under Germany's notorious "Holocaust denial" statute. www.revisionists.com/revisionists/zundel.html
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web