Running error Java eclipse -


when run code:

package jmv;  public class euler3 {     static int x=1;     static long z = 600851475143l;     public static void main(string[] args) {          system.out.println(isprime(123454321));     }      public static boolean isprime(int p){         for(int y=1 ; y<x ;){             if(x%y == 0){                 return true;              }else{                 return false;             }                    }         return false;     } } 

it returns false. why? thank help. appereciate every answer given appereiciate better ones more.

the fact not checking p, parameter given function isprime().

it should that:

package jmv;  public class euler3 {     static int x=1;     static long z = 600851475143l;     public static void main(string[] args) {         system.out.println(isprime(123454321));     }      public static boolean isprime(int p){         if (p%2 == 0) {             return false;         } else  {             for(int y=3 ; y<p ; y=y+2){                 if(p%y == 0){                     return false;                  }                       }         }         return true;      }  } 

you need check if 2 divides p first, because number may passed function. need check odd number >=3. basic mathematics , i'm sure forgot it.

doing save many tests. if test y++ checking every number , not necessary, if number , greater 2, not prime.

hope helps!


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -