+ 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.
4 odpowiedzi
+ 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"); }
+ 5
how do you say that it's not working. can you please explain @Kenneth
+ 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
0
It doesn't work with my code.
https://code.sololearn.com/c4RNVm4GauJ0/?ref=app