Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #195: We only support a 28000 bps connection.


comp / comp.lang.lisp / Re: Euler 14.

SubjectAuthor
* Euler 14.B. Pym
+* Re: Euler 14.HenHanna
|`* Re: Euler 14.Paul Rubin
| +- Re: Euler 14.Paul Rubin
| +- Re: Euler 14.HenHanna
| `- Re: Euler 14.HenHanna
`- Re: Euler 14.B. Pym

1
Subject: Euler 14.
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 28 May 2024 11:39 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Euler 14.
Date: Tue, 28 May 2024 11:39:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <v34fp4$iunh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 28 May 2024 13:39:17 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="45ccecf5734ae6486bc629ee94fff335";
logging-data="621297"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NwBXkAaz+z+XZLACj8WzX"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:8FoJeO3SEZsHOIBxc1DD31pJSgU=
View all headers

The following iterative sequence is defined for the set of positive
integers:

n -> n/2 (n is even)
n -> 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following
sequence:

13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1

It can be seen that this sequence (starting at 13 and
finishing at 1) contains 10 terms. Although it has not been
proved yet (Collatz Problem), it is thought that all starting
numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one
million.

Gauche Scheme

(use gauche.collection) ;; find-max

(define (cltz n) (if (odd? n) (+ 1 (* n 3)) (/ n 2)))

(define (d c n)
(if (= n 1) c (d (+ 1 c) (cltz n))))

(find-max (lrange 1 1000000) :key (pa$ d 1))

===>
837799

Subject: Re: Euler 14.
From: HenHanna
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 23 Jul 2024 19:24 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.lisp
Subject: Re: Euler 14.
Date: Tue, 23 Jul 2024 12:24:00 -0700
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <v7p00g$1b339$3@dont-email.me>
References: <v34fp4$iunh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 23 Jul 2024 21:24:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="45120b53e64fc89465e7bcc84dd55152";
logging-data="1412201"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yNV6IUdWCDUzFs1gF1cv4AAFQ4/OVs4w="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:yCH5W0MqdS37CibLrvAEQSF6pLw=
Content-Language: en-US
In-Reply-To: <v34fp4$iunh$1@dont-email.me>
View all headers

On 5/28/2024 4:39 AM, B. Pym wrote:
> The following iterative sequence is defined for the set of positive
> integers:
>
> n -> n/2 (n is even)
> n -> 3n + 1 (n is odd)
>
> Using the rule above and starting with 13, we generate the following
> sequence:
>
> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
>
> It can be seen that this sequence (starting at 13 and
> finishing at 1) contains 10 terms. Although it has not been
> proved yet (Collatz Problem), it is thought that all starting
> numbers finish at 1.
>
> Which starting number, under one million, produces the longest chain?
>
> NOTE: Once the chain starts the terms are allowed to go above one
> million.
>
>
> Gauche Scheme
>
> (use gauche.collection) ;; find-max
>
>
> (define (cltz n) (if (odd? n) (+ 1 (* n 3)) (/ n 2)))
>
> (define (d c n)
> (if (= n 1) c (d (+ 1 c) (cltz n))))
>
> (find-max (lrange 1 1000000) :key (pa$ d 1)) ===> 837799

is this fast?

what does the SUBJ line mean? ( Euler 14.)

Subject: Re: Euler 14.
From: Paul Rubin
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 23 Jul 2024 20:40 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.lisp
Subject: Re: Euler 14.
Date: Tue, 23 Jul 2024 13:40:10 -0700
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <87h6cf9711.fsf@nightsong.com>
References: <v34fp4$iunh$1@dont-email.me> <v7p00g$1b339$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Tue, 23 Jul 2024 22:40:11 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2be1b6de506d196ea9eb7967f2404084";
logging-data="1437571"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187fd073FNETIg1iav5LCdT"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:GOTcNtRnGNrS8tSzeqr8UVNBWok=
sha1:cfyOe8BqZyBitwiqqNiwjLUHmqs=
View all headers

HenHanna <HenHanna@devnull.tb> writes:
> is this fast?

No it is slow, it needs memoization

> what does the SUBJ line mean? ( Euler 14.)

It is problem 14 of projecteuler.net .

Subject: Re: Euler 14.
From: Paul Rubin
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Thu, 25 Jul 2024 18:28 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.lisp
Subject: Re: Euler 14.
Date: Thu, 25 Jul 2024 11:28:23 -0700
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <87o76lcomw.fsf@nightsong.com>
References: <v34fp4$iunh$1@dont-email.me> <v7p00g$1b339$3@dont-email.me>
<87h6cf9711.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 25 Jul 2024 20:28:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a89df0e331107fdc444e94bb37ae2dd9";
logging-data="2528263"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pvXPG737bt6q1LyGBfaC5"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:qUnRcHnl1aDP8wwihn2pBzZx/IY=
sha1:+JMB9qrp99OJMRL7YK5+ebh2+lA=
View all headers

Paul Rubin <no.email@nospam.invalid> writes:
> It is problem 14 of projecteuler.net .

Here is my solution, maybe not idiomatic CL since I don't write much of
that these days. It takes about 0.17 sec of user time on my laptop
using sbcl --script, or 1.1 sec with compiled clisp. It doesn't work
with interpreted clisp because the clisp interpreter has no TRO and so
the tail recursion overflows the stack. I could rewrite it iteratively
but nah. A similar C++ version with gcc -O3 takes about 0.03 sec. I
haven't experimented with changing the size of the memo table or
anything like that.
================================================================

(defun collatz (n)
(cond ((oddp n) (1+ (* 3 n)))
(t (floor n 2))))

(defvar memo (make-array 1000000 :initial-element nil))

(defun clen (n)
(cond ((= n 1) 1)
((<= n 0) 'crash)
((and (< n (length memo)) (aref memo n)) (aref memo n))
(t (let ((a (1+ (clen (collatz n)))))
(if (< n (length memo))
(setf (aref memo n) a))
a))))

(defun run (&optional (n 1) (mi 0) (ma 0))
(if (> n 1000000)
(list mi ma)
(let ((a (clen n))
(nn (1+ n)))
(if (> a ma)
(run nn n a)
(run nn mi ma)))))
(print (run))
(terpri)

Subject: Re: Euler 14.
From: HenHanna
Newsgroups: comp.lang.lisp
Organization: novaBBS
Date: Sun, 4 Aug 2024 20:01 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: HenHanna@dev.null (HenHanna)
Newsgroups: comp.lang.lisp
Subject: Re: Euler 14.
Date: Sun, 4 Aug 2024 20:01:37 +0000
Organization: novaBBS
Message-ID: <826321eb9fdd4e3ade5ce43b0f031cbc@www.novabbs.com>
References: <v34fp4$iunh$1@dont-email.me> <v7p00g$1b339$3@dont-email.me> <87h6cf9711.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1467686"; mail-complaints-to="usenet@i2pn2.org";
posting-account="25PjXUQKTQXKZnoxTqVufZcfCkmLjnu8AjjfHtuMysE";
User-Agent: Rocksolid Light
X-Face: P#KeQ)CUdd!==@fw~Ms1=,Hb`IWtb6:Mw)x3B=H1BfNC\lz?Nb&)M9}$>?'X7l;CuB}utlJ=PHsRBSG6X>dYZ$[>P]$~+`>@V6$t}hTLoQ7XC~W\>:`B3ALU]SH;d(\MEc}znW8m}-ma&yPFkJ2@KSQrz=!Y;><;6a>z6N+mt`ClCt.PAE<o+B$qjwejZSZ,w]^;vrdl24z5(pm={l,F10qRDF
X-Rslight-Site: $2y$10$9wBqpsrCvD0IavEtPdpAwOcM5CBQ50enfa7EVPF/qKhYTpEvP8mSO
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 5a1f1f09909a70d7ae18ae9af00e018f83ece577
View all headers

On Tue, 23 Jul 2024 20:40:10 +0000, Paul Rubin wrote:

> HenHanna <HenHanna@devnull.tb> writes:
>> is this fast?
>
> No it is slow, it needs memoization
>
>> what does the SUBJ line mean? ( Euler 14.)
>
> It is problem 14 of projecteuler.net .

THanks... it seems like a set of simple programming exercises

Subject: Re: Euler 14.
From: HenHanna
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 4 Aug 2024 20:11 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.lisp
Subject: Re: Euler 14.
Date: Sun, 4 Aug 2024 13:11:26 -0700
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <v8on9e$7hkd$1@dont-email.me>
References: <v34fp4$iunh$1@dont-email.me> <v7p00g$1b339$3@dont-email.me>
<87h6cf9711.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 04 Aug 2024 22:11:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="224edb0645f408051a984f84ed519886";
logging-data="247437"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18r+M6pca1OxITxxDE2WYNF8ul3Vo+zMnU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:QzJXh2RqfhA0nETxVpBjarNjku0=
In-Reply-To: <87h6cf9711.fsf@nightsong.com>
Content-Language: en-US
View all headers

On 7/23/2024 1:40 PM, Paul Rubin wrote:
> HenHanna <HenHanna@devnull.tb> writes:
>> is this fast?
>
> No it is slow, it needs memoization
>
>> what does the SUBJ line mean? ( Euler 14.)
>
> It is problem 14 of projecteuler.net .

thanks...

i just discovered this:

In 2003 We Discovered a New Way to Generate Primes
YouTube · Eric Rowland
396.1K+ views · 1 year ago

22:17 ... number theory (2011) (46 pages).
https://arxiv.org/abs/1101.4274 Eric Rowland, A natural prime-generating
recurrence, Journal of Integer ...

Subject: Re: Euler 14.
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 5 Aug 2024 02:24 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Euler 14.
Date: Mon, 5 Aug 2024 02:24:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <v8pd42$fdbc$1@dont-email.me>
References: <v34fp4$iunh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 05 Aug 2024 04:24:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7494c0c807b5ee450e56fbedd80570a2";
logging-data="505196"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Rm5ExhsFl7fXLjMydCrGw"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:EJJ2FGE9G+rvWcdY48zsuhEBDrY=
View all headers

B. Pym wrote:

> The following iterative sequence is defined for the set of positive
> integers:
>
> n -> n/2 (n is even)
> n -> 3n + 1 (n is odd)
>
> Using the rule above and starting with 13, we generate the following
> sequence:
>
> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
>
> It can be seen that this sequence (starting at 13 and
> finishing at 1) contains 10 terms. Although it has not been
> proved yet (Collatz Problem), it is thought that all starting
> numbers finish at 1.
>
> Which starting number, under one million, produces the longest chain?
>
> NOTE: Once the chain starts the terms are allowed to go above one
> million.
>
>
> Gauche Scheme
>
> (use gauche.collection) ;; find-max
>
>
> (define (cltz n) (if (odd? n) (+ 1 (* n 3)) (/ n 2)))
>
> (define (d c n)
> (if (= n 1) c (d (+ 1 c) (cltz n))))
>
> (find-max (lrange 1 1000000) :key (pa$ d 1))
>
> ===>
> 837799

newLISP

(define (cltz n) (if (odd? n) (+ 1 (* n 3)) (/ n 2)))

(define (d c n) (if (= n 1) c (d (+ 1 c) (cltz n))))

(local (best-n best-cnt cnt)
(for (n 1 999999)
(setq cnt (d 1 n))
(if (> cnt best-cnt) (setq best-n n best-cnt cnt)))
(list best-n best-cnt))

(837799 525)

1

rocksolid light 0.9.8
clearnet tor