I used C ++ I wrote a code to determine whether a prime number is a prime number, but today I use JAVA it takes a lot of time. I have to admit myself JAVA too much water, you need to study hard in the future.
the methods in the class are similar to the functions. I will use the methods to do this. Many people use two for loop statement , I think they are not using JAVA Object-oriented thinking, so I insist on using methods.
Here is myCode:
// Declare prime classes
Public class prime {
// Method for determining whether it is a prime number
Public Boolean isprime (INT number ){
For (INT I = 2; I <number; I ++ ){
If (Number % I = 0 ){
Return false;
}
}
Return true;
}
Public static void main (string [] Arg ){
Int n = 10000;
Int sum = 3;
Prime myprime;
Myprime = new prime ();
For (Int J = 3; j <= N; j ++ ){
If (myprime. isprime (j) = true) {// call Method
Sum + = J;
}
}
System. Out. println ("the prime number from 1 to 10000 is:" + sum );
}
}
Result:
The sum of prime numbers from 1 to 10000 is 5736397.