How can I set a list as divisors in a Java loop while the loop is running? | Sololearn: Learn to code for FREE!
+ 1

How can I set a list as divisors in a Java loop while the loop is running?

I got a program from Sanhit Sah. It was finding all primes up to " int". It used a loop to see if the prime was divisible by any number lower than current int. but you would only need to divide it by all primes lower than that number. 5%2 would be 1 so it would 5%4. Is it possible to eliminate this repetitiveness. I'm programing in Java and fairly new.

22nd Jun 2017, 7:39 PM
Kenneth Sorrells
Kenneth Sorrells - avatar
4 ответов
+ 5
check the condition till n/2. it will eliminate the repetition.. for(i=2;i<=n/2;i++) {        if(n%i==0){        System.out.println("Number is not prime");        flag=1;        break;        }      }       if(flag==0)       System.out.println("Number is prime");    }  
22nd Jun 2017, 7:54 PM
Safiya Yunus
Safiya Yunus - avatar
+ 5
how do you say that it's not working. can you please explain @Kenneth
22nd Jun 2017, 10:36 PM
Safiya Yunus
Safiya Yunus - avatar
+ 3
yeah they are alot of ways.. like an even number cannot be a prime number. also multiples of 5 cant be prime number. so you can condition it to not check them for prime numbers.. here is how i did it.. you can think of improving it even further https://code.sololearn.com/cvTcXW7Q81a9/?ref=app
22nd Jun 2017, 11:25 PM
adeel salim
adeel salim - avatar
22nd Jun 2017, 8:00 PM
Kenneth Sorrells
Kenneth Sorrells - avatar