My Code :
class MixFor5 {
public static void main (String [] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 3;
y = y - 2;
if (x == 6) {
break; // *Useless break;*
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
}
My output:
54 6
Can someone explain to me. Why when I remove break; my output data don't change at all.
You are never fulfilling the if(x==6)
lets take a look at the first loop:
int x = 0;
//....
x = x + 3; // x = 3;
if( x == 6 ) //false
break;
x = x + 3; // x = 6
now the second loop
x = x + 3 // x = 9
if( x == 6 ) //false x = 9
break;
x = x + 3; //x = 12
so you never are equal to 6 when comparing.
Related
My question is why output is 0 - 4? But not 0 - 9?
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
also next code doesn't compile?
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
Could you explain why?
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
The above prints 0 to 4 since x < 5 && y < 10 can only be true when both inequalities are true. So as soon as x == 5, the loop exits.
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
The above doesn't compile because you have already defined x as an int. The first part of the loop tries to redefine it as a long
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
The condition is not true when x == 5, x <5 && y <10, your condition is that x is less than 5, and y is less than 10, so the condition is not true when x == 5, if you change the condition to || will output 0-9
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
This does not compile because the x value has been defined, regardless of the type.The reason for this kind of error is because Java itself is not allowed, "Java designers think that doing so will cause program confusion"
I have been struggling with the break clause aspect of an exercise in the Java Headfirst book( CH5: p121 for reference). I understand how the below code works when the break clause isn't initiated but when it is, I don't get the results I expected.
Can someone please walk me through this?
I understand that the break clause should be activated when the input value is either x = x + 0 or x = x + 6
Below is the exercise code
class MixFor5 {
public static void main (String [] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 0; //input value
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println((x + " " + y));
}
}
My understanding would be that the inner loop would break if x == 6 and thus everything within the inner loop will discontinue including the additional x = x + 3
For x = x + 0. My expected result was x = 6 | y = 18 (actual result x = 6 | y = 14)
For x = x + 6. My expected result was x = 6 | y = 22 (actual result x = 60 | y = 10)
Thanks
For x = x + 0
outer = 0;
inner = 4;
x=x+0, x!=6;
x=x+3=3;
y=30-2=28;
inner = 3;
x=3;
y=28-2=26;
3 != 6;
x=3+3;
inner = 2;
x==6;
y=24;
break;
y=22;
outer=1;
inner=4;
x==6;
y=20;
break;
y=18;
outer=2;
inner=4;
x==6;
y=16;
break;
y=14;
for x=x+6
outer=0;
inner=4;
x=6;
y=28;
break;
y=26;
outer=1;
inner=4;
x=6+6+3;
y=24;
inner=3;
x=15+6+3;
y=22;
inner=2;
x=33;
y=20;
y=18;
outer=2;
inner=4;
x=42;
y=16;
inner=3;
x=51;
y=14;
inner=2;
x=60;
y=12;
y=10;
hope this would help.
public class R {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
int x = 0;
int y = 0;
int j = 0;
int distance = 0;
while (trials>j) {
j = j + 1;
int i = -1;
double counter = 1.0 * distance;
double sum = (distance + counter);
while (i<=n) {
i = i + 1;
if (i == n) {
distance = ((x*x) + (y*y));
}
if (i<n) {
int random = (int )(Math.random() * 4 + 1);
if (random == 1) x = x + 1;
if (random == 2) y = y + 1;
if (random == 3) x = x - 1;
if (random == 4) y = y - 1;
}
}
}
double average= (sum)/(trials);
System.out.println("mean " + "squared " + "distance " + "= " + average);
}
}
Hey guys I'm wondering how it's possible to compute a value within a loop, and then every single time the loop finishes (and the value in computed) to average them together. I can't wrap my head around the concept and I tried doing it in the code above but I can't quite figure it out.
As you can see there are two while loops, and inside one of them a random value (distance) is computed. So essentially I need to average the distances together, but I can't imagine how it's possible to add the distances that are computed each time together into one number. Let's say the loop goes through one time and outputs a singular distance, how would I go about adding a new distance (for the new loop) together with the old one, and then keep doing that for each trial?
You just have to divide the total distance per trials.
public class R {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
int x = 0;
int y = 0;
int j = 0;
int distance = 0, distance_total = 0;
while (trials>j) {
j = j + 1;
int i = -1;
distance = 0;
while (i<=n) {
i = i + 1;
if (i == n) {
distance += ((x*x) + (y*y));
}
if (i<n) {
int random = (int )(Math.random() * 4 + 1);
if (random == 1) x = x + 1;
if (random == 2) y = y + 1;
if (random == 3) x = x - 1;
if (random == 4) y = y - 1;
}
}
distance_total += distance;
}
System.out.println(distance_total/j);
}
}
I don't understand why the output is 25, from my wrong understanding obviously, i thought it is 20, because: first loop will be: i = 2; x = 5 and there will be 4 more loops since i <= m , therefor 5 x 4 = 20. I know i'm wrong but can't figure out where.
public class num {
public static void main(String[] args) {
int m, x, i;
x = 0;
i = 1;
m = 5;
while (i<= m){
x = x + m;
i = i + 1;
}
System.out.println(x);
}
}
Let's try a dry-run for this:
x=0;i=1;m=5
while (1<=5) ? yes, so x=0+5; i=2,
while (2<=5) ? yes, so x=5+5; i=3,
while (3<=5) ? yes, so x=10+5; i=4,
while (4<=5) ? yes, so x=15+5; i=5,
while (5<=5) ? yes, so x=20+5; i=6,
while (6<=5) ? no, so exit from loop
And therefore the result is: x=25
Whenver face this type situation, try debugging and printing. Most of the time you'll find your answqer:
public class num {
public static void main(String[] args) {
int m, x, i;
x = 0;
i = 1;
m = 5;
System.out.println("Initial : x = " + x + " and i = " + i);
while (i <= m) {
x = x + m;
i = i + 1;
System.out.println("x = " + x + " and i = " + i);
}
System.out.println(x);
}
}
Output:
Initial : x = 0 and i = 1
x = 5 and i = 2
x = 10 and i = 3
x = 15 and i = 4
x = 20 and i = 5
x = 25 and i = 6
25
Now I have completed the finding 23 sets of x y z values satisfy the condition x^3+y^3=1+z^3 & x
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long x = 1; setsFound < 23; x++) {
for (long z = x + 1; z<x*x; z++) {
long y = (long) Math.pow((1 + z*z*z - x*x*x), 1f/3f);
if (x * x * x == 1 + z * z * z - y * y *y && x<y && y<z) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
But the code I have is very inefficient, can anyone help me to fix this please?
Here is a working code:
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long z = 1; setsFound < 23; z++) {
for (long y = z - 1; y > 0; y--) {
long x = (long) Math.pow((1 + z * z * z - y * y * y), 1f/3f);
if(y <= x) break;
if (x * x * x == 1 + z * z * z - y * y *y) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
Couple of problems in the old one:
1/3 == 0 (because it's integer division) //use 1f/3f
x and z are swapped - you want z > x, not the other way around
(long)Math.pow(4*4*4, 1.0/3) == (long)3.9999999999999996 == 3 // use round
If you start the other way, with X < Y < Z by incrementing Y and Z up to a limit, you can gain some efficiencies. Once Z^3 > X^3 + Y^3 + 1, you can skip to the next Y value due to the concavity of the cubic function.
This implementation in C# works pretty fast on a laptop:
UInt64 setsFound = 0;
UInt64 xlim = 10000;
UInt64 ylim = 1000000;
UInt64 zlim = 10000000;
//int ctr = 0;
Console.WriteLine("The first 23 sets ordered by increasing x.");
Parallel.For(1, (long)xlim, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =>
//for (UInt64 i = 0; i < xlim; i++)
{
UInt64 x = (UInt64)i;
UInt64 xCu = x * x * x;
int zFails = 0;
for (UInt64 y = x + 1; y < ylim; y++)
{
UInt64 yCu = y * y * y;
zFails = 0;
for (UInt64 z = y + 1; z < zlim & zFails < 1; z++)
{
UInt64 zCu = z * z * z;
if (xCu + yCu - zCu == 1)
{
Console.WriteLine(String.Format("{0}: {1}^3 + {2}^3 - {3}^3 = 1", setsFound, x, y, z));
setsFound++;
}
else if (zCu > xCu + yCu - 1)
{
zFails++;
}
}
}
}
);
Obviously you can take out the parallelization. Also, here are the first 19 elements in that set (computer is still running, I'll try to post the last 4 later):
output http://desmond.yfrog.com/Himg640/scaled.php?tn=0&server=640&filename=8qzi.png&xsize=640&ysize=640