Teaching Kids Programming – Pythagorean Theorem and Algorithm to Find Pythagorean Numbers


Teaching Kids Programming: Videos on Data Structures and Algorithms

Proof-Pythagorean-Theorem Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video

Proof-Pythagorean-Theorem

Proof of Pythagorean Theorem

The big square’s area is tex_424ff74301dc9eb7f70297d1f12762ee Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video and it is equal to 4 triangls + a small square.

tex_151ab98edec1888592c85ec0adb37744 Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video
tex_9334edb744552737555bbad034e66a81 Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video
therefore: tex_6935a9501d0b0a64ca7dfde968328486 Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video

Bruteforce Algorithm to Find Pythagorean Numbers

Given c – a integer, let’s find all distinct positive pairs (a smaller or equal than b and tex_88b594a0cd6069d0d4cc26f8b5607d01 Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video

Then, we can iterate a from 1 till b is smaller than a. We can compute the value of tex_c09b485d3e83cfa990ebc76bbe769b0e Teaching Kids Programming - Pythagorean Theorem and Algorithm to Find Pythagorean Numbers algorithms math python teaching kids programming youtube video when a is fixed. Then, we just need to check if b is a perfect square – this can be done using inbuilt sqrt function or binary search algorithm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from math import sqrt
 
def f(c):
    a = 1
    while c * c >= 2 * a * a:
        b2 = c * c - a * a
        b = int(sqrt(b2))
        if b * b == b2:
            print("(%d)^2 + (%d)^2 = (%d)^2" % (a, b, c))
        a += 1
 
f(5)
f(10)
f(50)
f(100)
from math import sqrt

def f(c):
    a = 1
    while c * c >= 2 * a * a:
        b2 = c * c - a * a
        b = int(sqrt(b2))
        if b * b == b2:
            print("(%d)^2 + (%d)^2 = (%d)^2" % (a, b, c))
        a += 1

f(5)
f(10)
f(50)
f(100)

Output:

(3)^2 + (4)^2 = (5)^2
(6)^2 + (8)^2 = (10)^2
(14)^2 + (48)^2 = (50)^2
(30)^2 + (40)^2 = (50)^2
(28)^2 + (96)^2 = (100)^2
(60)^2 + (80)^2 = (100)^2

Finding Pythagorean Triplets in an Array – we can use Two Pointer Algorithm or Bruteforce with Hash Set: Teaching Kids Programming – Finding Pythagorean Triplets in Array using Two Pointer or Hash Set

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
542 words
Last Post: Algorithms to Sum using Distinct Positive Factorial Numbers
Next Post: Algorithms to Reverse a Graph (Adjacency List)

The Permanent URL is: Teaching Kids Programming – Pythagorean Theorem and Algorithm to Find Pythagorean Numbers

Leave a Reply