[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Challenge: Loop in Common Lisp and in Java

William James

8/10/2015 3:47:00 AM

Antonio Menezes Leitao wrote:

> Finally, a funny one (from CLTL): An infinite loop that only stops when
> it proves that Fermat's last theorem is (after all) false.
>
> (defun test-fermat ()
> (loop for z upfrom 2
> thereis
> (loop for n upfrom 3 below (log z 2)
> thereis
> (loop for x below z
> thereis
> (loop for y below z
> thereis (= (+ (expt x n)
> (expt y n))
> (expt z n)))))))

Gauche Scheme:

To avoid an infinite loop, we'll use "approx" instead of =.

(define (approx a b) (< (abs (- a b)) 2))

(use srfi-42 :only (first-ec))

(define (test-fermat)
(first-ec #f
(:list z (lrange 2))
(:range n 3 (round->exact (log z 2)))
(:range x z)
(:range y z)
(:let sum (+ (expt x n) (expt y n)))
(if (approx sum (expt z n)))
(list x y z n (list sum (expt z n)))))

===>
(9 10 12 3 (1729 1728))


--
Viewed at its most abstract level, a fundamental agenda is thus to influence
the European-derived peoples of the United States to view concern about their
own demographic and cultural eclipse as irrational and as an indication of
psychopathology. --- Dr. Kevin MacDonald; "The Frankfurt School of Social
Research and the Pathologization of Gentile Group Allegiances"