Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to condense my 9 if statements into one [closed]

Tags:

java

I wanted to see what the smallest number divisible by all one digit numbers was and instead of looking it up I created this.

public static void main(String[] args) {      for (int i = 100; i < 10000; i++) {          if (i % 2 ==0) {              if (i % 3 ==0) {                  if (i % 4 ==0) {                      if (i % 5 ==0) {                          if (i % 6 ==0) {                              if (i % 7 ==0) {                                  if (i % 8 ==0) {                                      if (i % 9 ==0) {                                          System.out.println(i);                                          break;                                     }                                 }                             }                         }                     }                 }             }         }     } } 

As you can see, I have an if statement in an if statement x9. The code worked but I wanted to condense my if statements using an array to make my if statement like this but it didn't work.

 if (i % x[1, 2, 3, 4, 5, 6, 7, 8]) {  System.out.println(i);  break;  } 

Any suggestions?

like image 869
getRect Avatar asked Apr 30 '18 19:04

getRect


People also ask

How do you avoid multiple nested if statements?

Nested IFs are powerful, but they become complicated quickly as you add more levels. One way to avoid more levels is to use IF in combination with the AND and OR functions. These functions return a simple TRUE/FALSE result that works perfectly inside IF, so you can use them to extend the logic of a single IF.

How many nested if statements is too many?

The limit is 7. However, it is possible to circumvent the limitation over the number of nested conditional formulas by cascading them. This article will look at the different methods for nesting multiple IF statements in Excel.

What is nested if statements?

Nested IF functions, meaning one IF function inside of another, allow you to test multiple criteria and increases the number of possible outcomes.


1 Answers

At first you would think you can test all of them at once by placing the product of 2 through 9 on the right side of the % operator.

if (i % (2 * 3 * 4 * 5 * 6 * 7 * 8 * 9) == 0) 

But because certain numbers include previous numbers in their factorization, you should use a lower number, specifically, the least common multiple. 8 is a multiple of 2 and 4, 9 is a multiple of 3, and if 8 and 9 are in the product, then 6 (2 * 3) is covered too.

if (i % (5 * 7 * 8 * 9) == 0) 

That turns out to be 2520, which is the least common multiple. It would much more readable to use 2520 and explain in a comment why this number is used.

/**  * The goal is to test if the number is a multiple of all integers  * from 2 through 9.  Mathematically, the least common multiple to is a  * multiple of all its input numbers.  Here, the LCM of 2, 3, ..., 9 is 2520.  */ public static final int LCM_2_THRU_9 = 2520; 

I've declared a constant and I'll use it here:

if (i % LCM_2_THRU_9 == 0) 
like image 76
rgettman Avatar answered Sep 23 '22 22:09

rgettman