Project Euler : Problem 009

Project Euler problems in Oforth . Feel free to post your own code.

Project Euler : Problem 009

Postby Franck » 30 Jul 2015 16:58

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 52.

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

Code: Select all
: pe009   ( -- n ) : Returns product
{
| a b c |
   1000 loop: a [
      a 1 + 1000 for: b [
         a sq b sq + sqrt dup asInteger dup ->c == ifFalse: [ continue ]
         a b + c + 1000 == ifTrue: [ a b * c * return ]
         ]
      ]
}
Franck
 
Posts: 144
Joined: 29 Oct 2014 19:01

Return to Project Euler in Oforth

Who is online

Users browsing this forum: No registered users and 1 guest