Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 34 lines (30 sloc) 0.888 kb
2ef59c7 polettix Added Project Euler #9.
polettix authored
1 # A Pythagorean triplet is a set of three natural
2 # numbers, a b c, for which,
eac20d1 Paul Cochrane Purge trailing whitespace from euler examples
paultcochrane authored
3 #
2ef59c7 polettix Added Project Euler #9.
polettix authored
4 # a**2 + b**2 = c**2
5 #
6 # For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2.
eac20d1 Paul Cochrane Purge trailing whitespace from euler examples
paultcochrane authored
7 #
2ef59c7 polettix Added Project Euler #9.
polettix authored
8 # There exists exactly one Pythagorean triplet for which a + b + c = 1000.
9 # Find the product abc.
10
11 use v6;
12
13 # $x + $y + $z = 1000 means that we can solve for $z:
14 #
15 # $z = 1000 - $x - $y
16 #
17 # We can safely assume that $x < $y < $z because the three numbers will be
18 # different and can be ordered. Hence, it suffices to iterate $x from 1 up
19 # to 1000/3, iterate $y from $x + 1 up to the midway to 1000 and get $z
20 # accordingly. This speeds up things in rakudo!
21 my $sum = 1000;
22 for 1 .. ($sum / 3) -> $x {
23 say $x;
24 for ($x + 1) .. (($sum + $x) / 2) -> $y {
25 my $z = $sum - $x - $y;
26 if ($z * $z == $x * $x + $y * $y) {
27 say $x * $y * $z;
28 exit;
29 }
30 }
31 }
c5fd1fc Paul Cochrane Add consistent vim coda to euler examples
paultcochrane authored
32
33 # vim: expandtab shiftwidth=4 ft=perl6
Something went wrong with that request. Please try again.