You are working on isPrime function
 
search
 


Contents:


swap function:
Swapping two values to change their addresses.
a

max function:

The largest number among two.
a

absolute value function:

The positive value of a number.
a

sorting function:

How to sort an array.
a

bisection function:

Bissection method used to solve equations by computing, example of cable strung ..
a

Newton-Raphson function:

To solve equations numerically, example of free fall motion ..
a

Secant function:

To solve equations numerically, example of free fall motion ..
a

isPrime function:

This function tests whether an integer is prime..
a

abundant function:

This function tests whether an integer is abundant..
a

factorial function:

Factorial of an integer..
a

quadratic function:

solving a quadratic equation ..
a

even_odd function:

To know if an integer is even or odd ..
a

mean value function:

To calculate the arithmetic mean of some values..
a

reverse function:

To reverse an integer and to know whether a string is palindrome..
a

least squares function :

To calculate the slope and the y_intercept of a linear equation ..
a

Monte Carlo method:

To calculate integrales or probabilities using Monte Carlo technique ..
a

2 and 3 equations systems solving :

To solve the 2 and 3 equations systems with 2 and 3 variables ..
a

Interpolation and Extrapolation :

To solve for lacking values in graphs or tables ..
a

Integer divisors :

Gives the divisors fo an integer and tests whether it is prime ..
a

Amicable pair :

Gives, if it exists, the amicable number of a given integer ..
a

Perfect numbers :

Tests whether a positive integer is perfect, and more ..
a

Periodic table of elements:

Periodic table of elements in C language, and more ..
a

Playing TicTacToe

Using some Fortran90 language funtionalities, and more ..
a


a
home

   isPrime function

	

Is it prime? function

1. The method /* Here isPrime function to test if the entered positive integer number is prime or not. The related algorithm is the following: int isPrime(int x){ int i; int is_prime=1; for(i = 2; i <= x/2; i++){ if((x % i) == 0) is_prime = 0; } return is_prime; } The integer 2 is prime. To test whether an integer "x" is prime, we calculate its modulo (x,n); n varies from 2 to x/2. Note that the division of x by the numbers between x/2 and x( x excluded) gives float numers between 2 and 1, then in this range, we have never mod(x,n) = 0. It is then reasonable to stop evaluating the modulo at x/2. 2. Example in C language: */ #include int isPrime(int x){ int i; int is_prime=1; for(i = 2; i <= x/2; i++){ if((x % i) == 0) is_prime = 0; } return is_prime; } int main() { float number; float decimal_part; int integer_part; printf("n Enter a positive integer: -->"); scanf("%f", &number); integer_part= (int)number; decimal_part = number - integer_part; if(number <=0) printf("n Enter a positive integer! n"); else if(decimal_part !=0) printf("n Enter an integer! n"); else if(isPrime(integer_part) == 1) printf("n The entered number %d is prime.n",integer_part); else printf("n The entered number %d is not prime.n",integer_part); return 0; } 3. Example in Fortran90 language: LOGICAL FUNCTION is_it_prime(Number) IMPLICIT NONE INTEGER :: Number INTEGER :: Div IF (Number == 2) THEN is_it_prime = .TRUE. ELSE IF (MOD(Number, 2) == 0) THEN is_it_prime = .FALSE. ELSE Div = 3 DO IF (MOD(Number,Div)==0) EXIT Div = Div + 2 END DO is_it_prime = Div == NUMBER ! Divide untill Divisor = Number, to make sure that the integer is prime END IF END FUNCTION is_it_prime ! Main program !------------- PROGRAM is_prime IMPLICIT NONE INTEGER :: Number LOGICAL :: is_it_prime !Get an integer: WRITE(*,*) "n Enter an integer: --> " READ(*,*) Number IF (is_it_prime(Number)) THEN WRITE(*,*) "The number", Number , " is prime. n" else WRITE(*,*) "The number", Number , " is not prime. n" END IF END PROGRAM is_prime


Custom Search


© 2007. The scientificsentence . All rights reserved.