All Pythagorean Triplets less than equal N in Optimal Time Complexity

All Pythagorean Triplets less than equal N in Optimal Time Complexity

Pythagorean Triplets are set of three ordered positive integers (x,y,z) such that

Pythagorean Triplets are set of three ordered positive integers (x,y,z) such that

                       x^2 + y^2 = z^2    --- (1)
x < y < z <= N

There are several techniques for generating Pythagorean Triplets for example using Gaussian Integers and using Euclid’s formula. They can be used to generate all primitive Pythagorean Triplets, which again can be used to generate all non-primitive Pythagorean triplets by using the fact that the non Primitive Pythagorean triples are a positive integer multiple of primitive Pythagorean Triplets.

Generating Pythagorean triplets simply using two loops by iterating over x and y for all possible x and y, and checking if they satisfy Equation (1), takes time O(N² ). Also using the above methods for generating primitive Pythagorean triplets and from them generating all Pythagorean triples may take time O(N² ).

I used Equation (2) which easily follows from Equation (1) by solving the quadratic equation for x, thus for “ x > 0 ”,

                      x = (b - a) + sqrt{2b(b - a)} --- (2)

a = y - x,
b = z - x and
0 < a < b

To find all Pythagorean Triples, we need to find the all values for a and b such that they are integers and Equation (2) holds. To find values for a and b, first, we will go step by step by finding ranges of each.

From the definition of a, it’s upper and lower limits depends upon b and can be written as

                             1 ≤ a ≤ b − 1

Since b > a and since the lower limit of a is 1 thus lower limit of b is 2. Now we need To find the upper limit of b.

we know 0< z ≤ N, from (b = z − x) _⇒ (_x + b ≤ N), thus from Equation (2) and a = b − 1,

                      (b − a) + sqrt{2b(b − a)} + b ≤ N           
                       ⇒ b + 1 + sqrt{2b} ≤ N
                       ⇒ 0 ≤ b ≤ N − sqrt{2N − 1}
Since first pythagorean Triple is (3,4,5), which makes N ≥ 5

Let,_ v = 2b(b − a), since _x is a positive integer and _1 < a < b, _thus from Equation (2),

                        2b ≤ v ≤ 2b(b − 1) and 
                        sqrt{v} ∈ N        and 
                        v (mod 2b) = 0

education python technology mathematics startup

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

The Basics of Python OS Module

The OS module is a python module that provides the interface for interacting with the underlying operating system that Python is running.