Personel Information

BSc: Physics Department, Middle East Technical University (METU), Ankara, Turkey
MSc: Physics Department, METU
PhD: Qualified, Physics Department, METU


Monday, July 11, 2016

PRIME OR NOT


## A funtion that takes a number
## in as an argument and returns
## the output p=1 if it is prime
## otherwise returns p=0.
function prime_or_not(num)
p=1; ## assume the number is prime.
div=2; ## the smallest divisor.
## since 2*num/2> num. Forbidden
Nsteps=num/2 ; ## Interval of division: the half
## of the num end itself
for i= div:Nsteps ## to divide al the numbers less than
## num/2.
if(rem(num,i)==0) ## any prime num cannot be
## divided with an integer.
p=0; ## otherwise exit the loop.
break;
endif
endfor
if(p == 1) ## prints the result
p=1
else
p=0
endif
endfunction

No comments:

Post a Comment