Recent Comments On Blog

rss

The beauty of the functional programming

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

#!/bin/env python import sys
import cmath def product (a, b, c):
        print a * b * c
        sys.exit ()
c = lambda a, b: abs (cmath.sqrt (a<strong>2 + b</strong>2))
f = lambda a, b, c: c % 1 == 0 and a + b + c == 1000
g = lambda a, b, c: f (a, b, c) and product (a, b, c) [ (a, b) for a in xrange (1, 1000)
        for b in xrange (a + 1, 1000) if g (a, b, c (a, b)) ]
 
Trackback URI: http://www.ceyusa.com/blog/index.php/trackback/517

#1 Re: The beauty of the functional programming

linxe, <E-Mail> / 15 August, 1:14pm  
avatar

#!/usr/bin/perl -w
use strict;

my @n = ( 1 .. 1000 );
foreach my $a ( @n ) {
my $a2 = $a * $a;
foreach my $b ( @n ) {
my $b2 = $b * $b;
my $c = sqrt ( $a2 + $b2 );
next unless ( ($a + $b + $c) == 1000 );
next unless ( ($c / int $c ) == 1 );
print "Solution: a = $a b = $b c = $c\n";
}
}

# La belleza de los numeros ...

[ Reply (0) ]

Leave a Comment

Write the captcha code you are seeing.

Comment XML feeds: RSS | Atom