[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: How faster can this be done?

William James

11/15/2015 2:13:00 AM

Wolfram Fenske wrote:

> (defun pythagorean-triplet ()
> (let ((c2-b2 0) (a 0) (i 0))
> (loop for c from 2 to 5000 do
> (loop for b from 1 below c do
> (progn
> (setf c2-b2 (- (* c c) (* b b))
> ;;a (isqrt c2-b2)
> a (truncate (sqrt c2-b2)))
> (when (= c2-b2 (* a a))
> (incf i)))))
> (print i)))

MatzLisp (Ruby):

def pyth_triplets
(2..5000).reduce(0){|sum,c|
sum +
(1...c).count{|b|
c2_b2 = c*c - b*b
a = Math.sqrt(c2_b2).to_i
c2_b2 == a*a}}
end

pyth_triplets
===>11362

--
In Stockholm ... 20 Muslim men ... began to assault the children, ripping their
swimsuits off.... [T]he men cornered one of the [11-year-old] girls in a
grotto in the bathhouse and gang-raped her. The police refused to press any
charges. www.liveleak.com/view?i=807_1369627137
1 Answer

William James

5/16/2016 10:28:00 PM

0

WJ wrote:

> Wolfram Fenske wrote:
>
> > (defun pythagorean-triplet ()
> > (let ((c2-b2 0) (a 0) (i 0))
> > (loop for c from 2 to 5000 do
> > (loop for b from 1 below c do
> > (progn
> > (setf c2-b2 (- (* c c) (* b b))
> > ;;a (isqrt c2-b2)
> > a (truncate (sqrt c2-b2)))
> > (when (= c2-b2 (* a a))
> > (incf i)))))
> > (print i)))
>
> MatzLisp (Ruby):
>
> def pyth_triplets
> (2..5000).reduce(0){|sum,c|
> sum +
> (1...c).count{|b|
> c2_b2 = c*c - b*b
> a = Math.sqrt(c2_b2).to_i
> c2_b2 == a*a}}
> end
>
> pyth_triplets
> ===>11362

OCaml:

let pyth_triplets () =
let count = ref 0 in
for c = 2 to 5000 do
for b = 1 to c - 1 do
let a = int_of_float (sqrt (float (c*c - b*b))) in
if a*a + b*b = c*c then incr count
done
done ;
!count ;;

--
[T]he organized Jewish community throughout the West has been a major force in
placing penalties on speech related to race, ethnicity, and immigration.
www.theoccidentalobserver.net/2016/03/could-it-happen-here/