Write following program in Java following guide: Background A Pythagorean triple
ID: 3750630 • Letter: W
Question
Write following program in Java following guide:
Background A Pythagorean triplet is three positive whole numbers a, b, and c, such that a +bc. For example, (3, 4, 5) is a Pythagorean triplet, since 3 +4-5 In this project, we will refer to the sum of a Pythagorean triplet to mean the sum of those three numbers So if a bc,then its sum is a+b+c. So for the Pythagorean triplet (3, 4, 5), its sum is 3+4+5 12. Description You will write a Java program that will repeatedly find all Pythagorean triplets for a given sum, or find the sum that has the most Pythagorean triplets. Here is a sample run of the program (user input is in red):Explanation / Answer
Hi,
i have attached the java code for your above problem .
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Proj3
{
public static void main(String[] args) throws IOException
{
int sum=0,a,b,i,j,k;
System.out.println("Choose an option below : 1)Find all the pythogorean triplets for a given sum 2)Find the sum that has the maximum pythogorean triplets 3)Quit ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int option = Integer.parseInt(br.readLine());
while (option!=3)
{
if(option ==1)
{
int flag =0;
int sumcheck=0;
System.out.println("Enter the sum ");
sum=Integer.parseInt(br.readLine());
for(i=1;i<sum/2;i++)
{
for(j=1;j<sum/2;j++)
{
sumcheck=(i*i)+(j*j);
for(k=1;k<sum/2;k++)
{
if (sumcheck==(k*k))
{
if((i+j+k)==sum)
{
flag++;
}
}
}
}
}
if(flag>1)
{
flag=flag/2;
}
System.out.println("Total number of solution is :"+flag);
}
if(option == 2)
{
int max_count=0;
int max_no=0;
int temp_count=0;
int temp_no=0;
int flag =0;
int temp_number=0;
int sumcheck;
System.out.println("ENter the bound ");
sum=Integer.parseInt(br.readLine());
for(i=1;i<sum/2;i++)
{
for(j=1;j<sum/2;j++)
{
sumcheck=(i*i)+(j*j);
for(k=1;k<sum/2;k++)
{
if (sumcheck==(k*k))
{
int sum1=i+j+k;
temp_count=0;
temp_number=sum1;
for(int temp_i=1;temp_i<sum/2;temp_i++)
{
for(int temp_j=1;temp_j<sum/2;temp_j++)
{
int temp_sumcheck=(temp_i*temp_i)+(temp_j*temp_j);
for(int temp_k=1; temp_k<sum/2; temp_k++)
{
if(i!=temp_i && j!=temp_j&& k!=temp_k)
{
if(temp_sumcheck==temp_k*temp_k)
{
if(sum1==(temp_k+temp_j+temp_i))
{
System.out.println(sum1);
temp_count++;
}
}
}
}
}
}
}
if (temp_count>max_count)
{
max_count=temp_count;
max_no=temp_number;
}
}
}
}
if (max_count !=0)
{
System.out.println("Best solution sum is ="+max_no+" with "+max_count+"different solution.");
}
}
if(option>2)
{
System.out.println("Wrong entry");
}
System.out.println("Choose an option below : 1)Find all the pythogorean triplets for a given sum 2)Find the sum that has the maximum pythogorean triplets 3)Quit ");
option = Integer.parseInt(br.readLine());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.