How to compare the given integer with possible roots - java

I could not name the question in the best way... My aim is to write a program that takes integer n from user. Then compare with the third power of two integers a and b.
If a^3 + b^3 is smaller than or equal to the given input, I want to print out every single possible calculation to the user.
My code is as follows:
System.out.println("Hi. Please insert an integer: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
double root = Math.cbrt(n);
int rootInt = (int) root;
for (int i = 0; i < rootInt; i++){
for (int j = 0; j < rootInt; j++){
if ((Math.pow(i, 3)) + (Math.pow(j, 3)) <= n){
double t = (Math.pow(i, 3) + (Math.pow(j, 3)));
int totalInt = (int) t;
System.out.println(i + "^3" + " + " + j + "^3" + " = " + totalInt );
} else {
}
j++;
}
i++;
}
When I run this and give an input as 30, it prints
0^3 + 0^3 = 0
0^3 + 2^3 = 8
2^3 + 0^3 = 8
2^3 + 2^3 = 16
What am I doing wrong?

You increase j and i twice. That's why you test only the even values for i, j.
If you want to correct the code, remove the i++, j++ from the end and use only the increments from the 2 fors.

Related

Printing with delimiter only between values

I have a slight problem with the output of my code and been searching for such topics same as with this but I don't find any.
while (true) {
System.out.print("Enter a positive integer: ");
n = sc.nextInt();
System.out.print(n + "! = ");
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
System.out.printf("%d x " , i);
}
System.out.println("");
}
The output must be. Whenever I type integer. e.g 5.
Enter a positive integer: 5
5! = 1 x 2 x 3 x 4 x 5
But the slight problem is that the output goes like this 5! = 1 x 2 x 3 x 4 x 5 x
There's extra x on the last number which should not be there
StringJoiner
Others already answered how to fix your code but I would like to provide you with a more professional solution, namely by using a StringJoiner.
With this, you can give a delimiter, prefix and suffix, then just add all your elements and the StringJoiner will make sure that the delimiter is only added in between. It takes all the work from you. Here is the code:
StringJoiner sj = new StringJoiner("x ", n + "! = ", "");
for (int i = 1; i <= n; i++) {
sj.add(Integer.toString(i));
}
System.out.println(sj);
Streams
If you prefer streams:
String result = IntStream.rangeClosed(1, n)
.mapToObj(Integer::toString)
.collect(Collectors.joining("x ", n + "! = ", ""));
System.out.println(result);
Replace this line:
System.out.printf("%d x " ,i);
by this:
if (i == n)
System.out.printf("%d" ,i);
else
System.out.printf("%d x " ,i);
This way you avoid printing "x" when you print the last number in the factorization.
Perhaps you can do this:
while (true) {
System.out.print("Enter a positive integer: ");
n = sc.nextInt();
String result = "";
System.out.print(n + "! = ");
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
result += i + " x ";
}
System.out.println(result.substring(0, result.length() - 3);
}

What kind of loop bug does this code have and how to fix it?

Just wondering where exactly is the bug? Is the for statement formatted incorrectly?
int n;
sum = 0;
for (n = 1; n < 10; n++) {
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
You need to close your for loop with a } and declare the sum variable as an int. So the code should look like:
int n;
int sum = 0;
for (n = 1; n < 10; n++){
sum = sum + n;
}
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
You should research using an IDE like Eclipse. Its a text editor for your code that will point out where the compilation errors are.

Math.random won't randomize number 9 whenever it has to be assigned to last array slot

My code creates an array of size 10, it randoms numbers from 0 to 9 to fit in each slot. The problem comes when the number 9 is not picked until the last space. Math.random keeps randomizing numbers but it will never pick the number 9. I ran the program for about 1 minute and it never picked it.
Here is my program
public class GenerateRandomNumbers{
// main method
public static void main(String[] args) {
int aSize = 10;
int[] a = new int[aSize];//setting size of array
for(int i = 0; a.length > i; i++){//looping through the whole array
a[i] = (int)(Math.random()*9) + 1;//assigning random number to each slot of array
System.out.println("assign " + a[i] + " to i" + i);
//looping through filled array slots.
for(int k = i-1; -1 < k; k--){
System.out.println("Check if " + a[i] + " i"+ i + " = " + a[k]+ " k"+ k );
//if not unique give a new number
if(a[i] == a[k]){
System.out.println("CHANGE HERE");
a[i] = (int)(Math.random()*9) + 0;
System.out.println("assign " + a[i] + " to " + i);
k = i;//reset loop so it checks all over again
}
}
System.out.println("ACCEPT");
}
for(int i = 0; a.length > i; i++){
System.out.println(a[i]);
}
}
}
Can someone explain me what is causing the bug?
Your line a[i] = (int)(Math.random()*9) + 0; is different from the time you used Math.random() above. Above you said (int)(Math.random()*9) + 1, that will give you a random number in the range of [1,9].
(int)(Math.random()*9) + 0 will never evaluate to 9, its range is [0,8].

java get even or odd number

I have a assignment for school: I need to get even or odd numbers and I made something to work for numbers that are above zero, but I need to find out how a negative number is even or odd.
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
int x = i;
while (x > 1) x = x - 2;
System.out.println(i + evenodd[x]);
}
simply use module % operator
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
int x;
if(i%2==0){
x=0;/*pass 0 to print even number */
}else{
x=1;/*pass 1 to print odd number*/
}
System.out.println(i + evenodd[x]);
}
Even and Odd numbers:
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
System.out.println(i + evenodd[i % 2]);
}
OR
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
if(i % 2 == 0)
System.out.println(i + evenodd[i % 2]);
else
System.out.println(i + evenodd[i % 2]);
}
For negative numbers is even or odd?:
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i > -100; i--)
{
if(i % 2 == 0)
System.out.println(i + evenodd[0]);
else
System.out.println(i + evenodd[1]);
}
Try and use the % operator.
Usage : x % y -> This will give you the remainder when y is divided by x.
In your case use y = 2. Hence if the remainder = 0, the number is even else the number will be odd.
I am not giving you the exact code, since this is your assignment. Try it yourself based on the suggestion.
To find out odd and even number. Follow the logic
divide your number by 2 if the reminder is 0 then it is even
else it is odd
this similar for both positive and negative numbers
to find out reminder use modulo division operator (%). if your_number%2=0 your_number is even else your_number is odd.
Sample code:
for(int i=1;i<100;i++){
if(i%2==0){//for positive number
System.out.println(i+ " is Even Number");
}else{
System.out.println(i+ " is Odd Number");
}
if((-1)*i%2==0){//for negative number
System.out.println((-1)*i+" is Even Number");
}else{
System.out.println((-1)*i+" is Odd Number");
}
}

Nested For Loop and specific array element searching

I have a question on the method and process of how to look at these generated arrays.
Basically I want to create an array of [a,b,c,(a+b+c)] and as well as a second array of [d,e,f,(d+e+f)] and if the third element in array1 and array2 are the same, display the arrays to strings.
int num = 10;
for(int a = 0; a < num; a++){
for(int b = 0; b < num; b++){
for(int c = 0; c < num; c++){
if(a<=b && b<=c){
arrayOne[0] = a;
arrayOne[1] = b;
arrayOne[2] = c;
arrayOne[3] = (a+b+c);
}
}
}
}
for(int d = 0; d < num; e++){
for(int e = 0; e < num; e++){
for(int f = 0; f < num; f++){
if(d<=e && e<=f){
arrayTwo[0] = d;
arrayTwo[1] = e;
arrayTwo[2] = f;
arrayTwo[3] = (f -(d+e));
}
}
}
}
as you can see I am beyond stump.I am not quite sure where i can get each iteration of the arrays and compare the values by matching the sums in each array and as well as displaying the respective array they are in. Thank you all in advanced.
If I understand your question correctly if a=1, b=3, c=4 and d=2, e=3, f=3 you'd like to print something along the lines of 1 + 3 + 4 = 8 = 2 + 3 + 3. First, what you're doing right now is creating two arrays like Floris described in the comment. What you want to do is store all the values in one array of arrays, as follows:
int max; \\ To determine the value of max see the edit below.
int array[][] = new int[max][num];
int index = 0;
for (int a=0; a < num; a++) {
for (int b=a; b < num; b++) {
for (int c=b; c < num; c++) {
array[index][0] = a;
array[index][1] = b;
array[index][2] = c;
array[index][3] = a + b + c;
index++;
}
}
}
for (int i = 0; i < max; i++) {
for (int j = i; j < max; j++) {
if (array[i][3] == array[j][3]) {
string outString = array[i][0] + " + " + array[i][1] + " + " + array[i][2] + " = " + array[i][3] + " = " + array[j][0] + " + " + array[j][1] + " + " + array[i][2];
System.out.println(outString);
}
}
}
You can see that I improved performance by starting b from a and c from b since you are throw out all the values where b < a or c < b. This also should eliminate the need for your if statement (I say should only because I haven't tested this). I needed to use an independent index due to the complexities of the triple nested loop.
Edit 2: Ignore me. I did the combinatorics wrong. Let An,k be the number of unordered sets of length k having elements in [n] (this will achieve what you desire). Then An,k = An-1,k + An,k-1. We know that An,1 = n (since the values are 0, 1, 2, 3, 4, ..., n), and A1,n = 1 (since the only value can be 11111...1 n times). In this case we are interested in n= num and k = 3 , so plugging in the values we get
A_num,3 = A_num-1,3 + A_num,2
Apply the equation recursively until you come to an answer. For example, if num is 5:
A_5,3 = A_4,3 + A_5,2
= A_3,3 + A_4,2 + A_4,2 + A_5,1
= A_3,3 + 2(A_4,2) + 5
= A_2,3 + A_3,2 + 2(A_3,2) + 2(A_4,1) + 5
= A_2,3 + 3(A_3,2) + 2(4) + 5
= A_1,3 + A_2,2 + 3(A_2,2) + 3(A_3,1) + 2(4) + 5
= 1 + 4(A_2,2) + 3(3) + 2(4) + 5
= 1 + 4(A_1,2) + 4(A_2,1) + 3(3) + 2(4) + 5
= 1 + 4(1) + 4(2) + 3(3) + 2(4) + 5
= 5(1) + 4(2) + 3(3) + 2(4) + 5
It looks like this may simplify to (num + (num - 1)(2) + (num - 2)(3) + ... + (2)(num - 1) + num) which is binomial(num, num) but I haven't done the work to say for sure.
int givenNumber = 10;
int []arrayOne = new int [4];
int []arrayTwo = new int [4];
int count = 0;
for ( int i = 0; i < givenNumber; i ++)
{
for ( int x = 0; x < givenNumber; x ++ )
{
for ( int a = 0; a < givenNumber; a++ ){
arrayOne[0] = (int)(a * java.lang.Math.random() + x);
arrayOne[1] = (int)(a * java.lang.Math.random() + x);
arrayOne[2] = (int)(a * java.lang.Math.random() + x);
arrayOne[3] = (int)(arrayOne[0]+arrayOne[1]+arrayOne[2]);
}
for ( int b = 0; b < givenNumber; b++ ){
arrayTwo[0] = (int)(b * java.lang.Math.random() + x);
arrayTwo[1] = (int)(b * java.lang.Math.random() + x);
arrayTwo[2] = (int)(b * java.lang.Math.random() + x);
arrayTwo[3] = (int)(arrayTwo[0]+arrayTwo[1]+arrayTwo[2]);
}
if (arrayOne[3] == arrayTwo[3])
{
for ( int a = 0; a < 2; a++ )
{
System.out.print(arrayOne[a] + " + ");
} System.out.print(arrayOne[2] + " = " + arrayOne[3] + " = ");
for ( int a = 0; a < 2; a++ )
{
System.out.print(arrayTwo[a] + " + ");
} System.out.print(arrayTwo[2]);
System.out.println("\n");
count += 1;
}
}
}
if (count == 0)
System.out.println(
"\nOops! you dont have a match...\n" +
"Please try running the program again.\n");

Categories