Pythagorean Triple

Sort by

recency

|

31 Discussions

|

  • + 0 comments
    def pythagoreanTriple(a):
        if a % 2 != 0:
            b = (a*a - 1) // 2
            c = b + 1
        else:
            m = a // 2
            b = m*m - 1
            c = m*m + 1
        return a, b, c
    
  • + 0 comments

    What's up? I've come to introduce you to coreball. They're a great option for individuals who want to work out in the comfort of their own homes. In addition, it is a viable alternative to providing your body with beneficial exercises while in quarantine. Try it out!

  • + 0 comments

    def pythagoreanTriple(a): if a%2 ==1 : n= (a-1)//2 m=n+1 else : if a % 4 == 0: m = a//4 + 1 n = a//4 -1 else : n=1 m=a//2 return [m**2-n**2,2*m*n,m**2+n**2]

  • + 0 comments

    Complete Java Solution

    import java.io.*;
    import java.util.*;
    import java.math.BigInteger;
    
    
    public class PythagoreanTriple
    {
    	public static void main(String[] args) 
    	{
    		Scanner sc=new Scanner(System.in);	
    		BigInteger b=sc.nextBigInteger();
    
    if(!b.mod(new BigInteger("2")).equals(BigInteger.ZERO))
    		{
    			//if odd
    			BigInteger c=b.pow(2);
    			c=c.subtract(new BigInteger("1"));
    			c=c.divide(new BigInteger("2"));
    			BigInteger a=c.add(new BigInteger("1"));
    			System.out.println( b +" "+a +" "+c);
    		}
    		else
    		{
    			///if even
    		BigInteger c=b.divide(new BigInteger("2"));
    			c=c.pow(2); 
    			c=c.subtract(new BigInteger("1"));
    			
    		BigInteger a=c.add(new BigInteger("2"));
    			System.out.println( b +" "+a +" "+c);
    		}
    	}
    }
    
  • + 0 comments
    1. If a is odd then a^2 is also odd. Now there exists a k, such that, a=2k+1 Therefore a^2=(2k+1)^2=4k^2+4k+1=2b+1, where b=2k^2+2k Hence, a^2=2b+1=(b+1)^2-b^2. i,e, a^2+b^2=c^2, where c=b+1
    2. If a is even then there exists an m such that 2m=a. We know that, (m^2-1)^2+(2m)^2=(m^2+1)^2 , hence a=2m , b=m^2-1 and c=m^2+1.