Re: Euler 14.

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Euler 14.
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 05. Aug 2024, 04:24:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8pd42$fdbc$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
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)
 


Date Sujet#  Auteur
28 May 24 * Euler 14.7B. Pym
23 Jul 24 +* Re: Euler 14.5HenHanna
23 Jul 24 i`* Re: Euler 14.4Paul Rubin
25 Jul 24 i +- Re: Euler 14.1Paul Rubin
4 Aug 24 i +- Re: Euler 14.1HenHanna
4 Aug 24 i `- Re: Euler 14.1HenHanna
5 Aug 24 `- Re: Euler 14.1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal