Euler Problem 9 : Special Pythagorean Triple

Euler Problem 9 Definition

Euler Problem 9: Pythagorean Triples

Scatter plot of the legs (a,b) of the first Pythagorean triples with a and b less than 6000. Negative values are included to illustrate the parabolic patterns. By Dearjean13Own work, CC BY-SA 4.0, Link

A Pythagorean triple 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 = 5^2 .

There exists exactly one Pythagorean triplet for which a + b + c = 1000 .

Find the product of a, b and c.

Brute Force Solution

This solution uses brute force and checks all combinations of a, b and c. To limit the solution space I used the fact that a < b < c, which implies that a < s/3,  and a < b < s/2, where s is the sum of the three sides.

a <- 0
b <- 0
c <- 0
s <- 1000
found <- FALSE
for (a in 1:floor((s/3))) {
    for (b in a:(s/2)) {
        c <- s - a - b
        if (a^2 + b^2 == c^2) {
            found <- TRUE
            break
        }
    }
    if (found) 
        break
}
answer <- a * b * c

View this code on GitHub.

 

4 thoughts on “Euler Problem 9 : Special Pythagorean Triple

  1. Pingback: Euler Problem 9 : Special Pythagorean Triple - Use-R!Use-R!

  2. That was fun.

    You could speed that up slightly by having b loop through (a+1):(s/2) instead of a:(s/2) since b can’t equal a.

    I tried a different tack. Since min(a)=1 and max(b)=499, I walked a and b towards each other. That way there’s a max of 498 possibilities instead of 292*497 in yours.

    I ran yours and mine 100 times and collected the times.

    Your code:
    user system elapsed
    19.38 0.14 19.61

    Mine:
    user system elapsed
    0.11 0.10 0.22

    Neither of our codes verifies that c is an integer, and if there were a second solution, they would both miss the second.

    # Your code slightly modified
    ptm <- proc.time()
    for (round in 1:100) {
        a <- 0
        b <- 0
        c <- 0
        s <- 1000
        found <- FALSE
        for (a in 1:floor((s/3))) {
            for (b in (a+1):(s/2)) {
                c <- s - a - b
                if (a^2 + b^2 == c^2) {
                    found <- TRUE
                    break
                }
            }
            if (found) break
        }
        answer <- a * b * c
    }
    proc.time()-ptm
    
    # Mine
    ptm <- proc.time()
    a <- 1
    b <- 499
    repeat{
        c <- sqrt(a^2 + b^2)
        if (a + b + c > 1000) {b <- b-1}
        else if (a + b + c < 1000) {a <- a+1}
        else if (a + b + c ==1000) {break}
    }
    answer <- a * b * c
    proc.time()-ptm
    
    • Thanks Alan,

      Project Euler is indeed a lot of fun. I am aiming to do at least 100 problems.

      I knew there had to be faster code, so thanks for contributing.

      I toyed with the Euclidean formulas for generating triples but moved to the next problem.

      Peter

  3. Pingback: Euler Problem 9 : Special Pythagorean Triple – Mubashir Qasim

Feel free to share your thoughts about this article