Menu

1 / 2

PWC 393 Triangles and Primes

DEV Community: perl·Bob Lied·5 days ago
#aa5i4oz6
#devtoperl#devto#dev#task#prime
Reading 0:00
15s threshold

A couple of quick mathematical tasks for this week's challenges.

Task 1: Pythagoras Multiplied

Task Description

You are given a positive integer n . Find the number of all positive integer triplets ( a,b,c ) so that a 2 + b 2 = c 2 and a , b , and c are integers <= n .

Example 1: n=20 --> 12

(3,4,5),(4,3,5),(5,12,13),(6,8,10),(8,6,10),(8,15,17),(9,12,15),(12,5,13),(12,9,15),(12,16,20),(15,8,17),(16,12,20)

Example 2: n=7 --> 2

Example 3: n=1 --> 0

Example 4: n=15 --> 8

Example 5: n=30 --> 22

Task Discussion

We notice from Example 1 that order matters: (3,4,5) is counted as distinct from (4,3,5).

We know from the geometry of right triangles that there are no integer solutions where a = b (an equilateral right triangle has hypotenuse sqrt(2)*a ), so all valid triples will have a ≠ b ≠ c .

I'm going to start by using brute force, and then thinking about optimizing. For every c from 1 to n , there is a target hypotenuse c 2 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.