I need help with the following Problem:
Create the program that calculates and prints all possible pairs of the following equation (You can use Math.sqrt(double) in your solution but not Math.square )
n>=a>1
n>=b>1
Where (a^2 + b^2 = c^2)
For example if n = 4 , then I'm printing :
System.out.println(a = 3 , b =4 or a = 4 and b =3 ; 3^2 + 4^2=25; we found 1 pair) because: 4>=4>1
4>=3>1
4^2 +3^2 =25
16+9=25 ,so this is one pair of a and b (a= 3 and b= 4 and a= 4 and b= 3 is the same ,so it only gives me one pair )
Another example is: n =144 , then 144>=144>1 and 144>=17>1
144^2 + 17^2 = 21025 , so we found first pair /.
n = 144 , then 144 >=143>1 and 144 >=24>1
143^2 + 24^2 = 21025 ,we found the second pair /.
(n= 144 is giving all previous pairs of numbers , not only those that i'm mentioned)
So I started from Method main and accompished a few things but recently can't copy the sum of the two arrays: (cel1 and cel2_copy , their sum is equals to ) to another array(tempofsum1) properly.
public static void main(String[] args)
{
Scanner inp = new Scanner (System.in);
System.out.println(" Input the value of n ");
int n = inp.nextInt();
int counter = 0 ;
//counts the possible number of digits that gonna insert into my original array
int counter2 = 0;
//counts the number of times that our sum is calculated
int i , j,newroot; // newroot of 25 = 5 , root of 25 = 5.0
int sumofsquare = 0; //(a^2 + b^2)
int defaultsum =0; // (a+b)
int [] tempofsum2,tempofsum1 ;
for (i = 2; i<=n;i++)
{
//counts number of digits that i'm gonna insert into my original array
counter ++;
}
System.out.println("We have to sort out " + counter + " numbers inside our original array \n" );
int [] cel1 = new int [counter] ;// Our Original Array:
//all numbers in our array are equal to zero at the moment
for ( j = 0 , i =2 ; j<cel1.length ;j++ ,i++ )
{
//loop that insersts the numbers that I calculated
// i = 2 is first number in our array ,becasue numbers in the range of n > 1
cel1[j] = i ;
//number at the index of 0 is now 2 , this row is sorting our array numbers in the ascending order
System.out.println("Value of the original array at the index of # " +j+ " is " +cel1[j]);
}
System.out.println(" Our N is " + n );
// I'm creating my second array,
int [ ] cel2_copy = new int [cel1.length];
for (i = 0;i< cel1.length;i++)
{
//copying values from Original Array to this new .
cel2_copy [i] = cel1 [i] ;
}
//Our For "Loop", that calculates all possible summary of cel2_copy and cel1 arrays , pythagoras triplets, their roots
for (i = 0 ; i<cel1.length ;i++)
{
for ( j = 0 ; j < cel1.length; j++ )
//cel1[i] = a and cel2_copy[j] = b (in our Pythagoras equation)
sumofsquare = cel1[i]*cel1[i] + cel2_copy[j]*cel2_copy[j];
//summing our a^2 + b^2
defaultsum = cel1[i] + cel2_copy[j];
//same as previous row but a + b , not a^2 + b^2
double root = Math.sqrt (sumofsquare);
//calcutates the root of a^2 + b^2
newroot = (int ) root;
/*
cel1[i] = a , cel2_copy[j]=b
Our If statemenet that gonna check if ( n >= a >1 && n>= b > 1 )and if (our
integer root of a^2 +b^2 equals to its double value)if it is ,that means
that we have sum that can be rooted properly.
*/
if ((((n > cel1[i] || n == cel1[i]) && ( ( cel1[i] >1 )
&& (n > cel2_copy[j] || n == cel2_copy[j])
&& ( (cel2_copy[j] >1 )))
&&( newroot * newroot == root * root ))))
{
This is my Originofsum array that gets the summary of "cel1" array and "cel2_copy" array
originofsum [i] = sumofsquare ;
counter2 ++;
//counter2 calculates the number of times that we get summary`
System.out.println("The sum is calulated " +counter2 + " times " );
System.out.println(sumofsquare + " is sumofsquare " + " at the index of i = " +i +" and the index of J = " +j );
System.out.println("Our sum of a = " +cel1[i]+ " and b = " + cel2_copy[j] + " is a+b =" +defaultsum);
System.out.println("The double root of sumofsquare =" +root );
System.out.println("The int root of sumofsquare = " +newroot );
System.out.println("\n");
}// end of If
}//end for J
}// end for I
So here I'm trying to copy my sum values from array "OriginofSum" to "tempofsum1" ,but that not works ,
In console(if you press at the start of the program 4(4 is N )printed following numbers :0 25
We get two numbers because my only two sums in the range from 2 to 4 ( n = 4 and n >=a>1 and n>=b>1)that giving me proper square root is a= 3 ,b= 4 and a= 4 and b= 3 (16+9=25) and (9+16=25)
So my array missed number 25 one time.
IF you press 14 , you get sum 8 times : 0 25 25 169 100 0 100 225
So now my array missed number 169 one time and number 225 one time.(at their places is 0 value).
System.out.println("Sum is calculted " +counter2 + " times " );
tempofsum2 = new int [counter2];
tempofsum1 = new int [ counter2];
for ( i = 0 ; i <tempofsum1.length;i++ )
{
tempofsum1 [i] = originofsum[i];
System.out.println(originofsum[i]);
} // end of For i Loop
}// end of Main
}// end of Class
I"l be happy to get your help , but remeber that it was a test question ,so I should stick to a basis. Thank you in advance.)
If your goal is simply to find numbers that form Pythagorean triples with another integer, this seems pretty straightforward to me:
private static void printPairs(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
long sum = (i * i) + (j * j);
double root = Math.sqrt(sum);
if (root == ((int) root)) {
System.out.printf("Found pair (%d, %d): %d^2 + %d^2 = %d^2\n",
i, j, i, j, (int) root);
}
}
}
}
Example output:
Found pair (3, 4): 3^2 + 4^2 = 5^2
Found pair (4, 3): 4^2 + 3^2 = 5^2
Demo: https://ideone.com/dj1w0M
This doesn't require any arrays or anything really complicated. Am I missing something?