Answers for "Special Pythagorean Triplet"

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
0

c Pythagorean triples

#include <stdio.h>
#include <stdlib.h>

int main() {
	int a, b, c;

	printf("TERNE PITAGORICHE!\n\nPrimo numero: ");
	scanf("%d", &a);
	
	printf("Secondo numero: ");
	scanf("%d", &b);
	
	printf("Terzo numero: ");
	scanf("%d", &c);
	
	if((a * a + b * b) == (c * c))
	{
		printf("E' una terna pitagorica!\n\n\n");
	}
	else
	{
		printf("Non e' una terna pitagorica!\n\n\n");
	}
	
	system("pause");
}
Posted by: Guest on February-11-2021

Python Answers by Framework

Browse Popular Code Answers by Language