You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + n;1;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + n;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + n;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + n;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
You can greatly reduce the complexity of your code, by using the ternary operator:
while (n != 1){
stepsTaken++;
n = (n % 2 == 0) ? n / 2 : n * 3 + n;
System.out.print(n);
//Your largest number and the linebreaks go here;
}
I removed the counter++
as you correctly mentioned, you can instead of counter == 9
use stepsTaken % 9 == 0
###Small explanation:
this:
int i = {some value};
if(i % 2 == 0){
i = i / 2;
}
else {
i = i * 3 + 1;
}
can be shortened to the terary operator. It has the following syntax:
i = (condition) ? if-branch : else-branch;
That is the whole magic behind the shortened code.