Answers for "pythagorean triplet"

1

pythagorean triplet

#include<bits/stdc++.h>
using namespace std;

bool check(int a,int b,int c){
    int y,z;
    int x = max(a,max(b,c));
    if(x == a)
    {
        y=b;
        z=c;

    }
    if(x == b)
    {
        y=a;
        z=c;
    }
    if(x == c)
    {
        y=a;
        z=b;
    }
    if((x*x)==(y*y) + (z*z)){
        return 1;
    }
    else{
        return 0;
    }
}

int32_t main(){
    int a,b,c;
    cin>>a>>b>>c;
    if(check(a,b,c)){
        cout<<"Pythagoras triplet";
    }
    else{
        cout<<"Not a Triplet";
    }
    return 0;
}
Posted by: Guest on June-08-2021
0

Special Pythagorean Triplet

# Project Euler  -  Question 9  -  Special Pythagorean Triplet
# Written by Matthew Walker, 20 August 2017

# https://projecteuler.net/problem=9
# A Pythagorean triplet is a set of three natural numbers,
#   a < b < c, for which, a2 + b2 = c2
# For example, 32 + 42 = 9 + 16 = 25 = 52.
# There exists exactly one Pythagorean triplet for which
#   a + b + c = 1000.  Find the product abc.
# Answer = 31,875,000  Took 21 seconds. (10s with exit())

# Iterate a from 1 to 1000
# Then iterate b from a to 1000
# Then iterate c from b to 1000
# This is because a < b < c. Otherwise it would take
#  much much longer and return two answers
for a in range(1,1000):
	for b in range(a,1000):
		for c in range (b, 1000):
			# Once we have an iteration of a, b, and c
			# Determine if it fits the criteria of a+b+c==1000 and
			#  a^2 + b^2 == c^2
			# Test a+b+c first because it is a faster test.  Takes ~half the time
			if (a+b+c == 1000):
				if (a*a + b*b == c*c):
					# Print answers
					print('A: ' + str(a) + ' B: ' + str(b) + ' C: ' + str(c))
					print('Product is: ' + str(a*b*c))
					# If we found it, exit to save time
					exit()
Posted by: Guest on October-10-2021

Browse Popular Code Answers by Language