Algorithms

Pi19404
December 16, 2012

Contents

Contents
Prime Numbers 4

3|6

Prime Numbers

Prime Numbers
A number n is divisible by another number f if there exists the number q such that n=f*q where f is called the factor and q is quotient. A prime number has exactly two factors 1 and itself. A composite number is a number that has factors other than 1 and itself. A perfect number is equal to sum of its factors. Every natural number greater than one has a unique prime factorication. It can be uniquely expressed as product of prime numbers. prime number are infinite set Below is the python code for brute force method to check if a given number if prime number or note
1

3

# ! / usr/bin/python """ " Program t o check i f number i s prime or not Created on S a t Dec 15 1 9 : 5 8 : 0 8 2012 @author : p i 1 9 4 0 4 """ n=23 f o r i i n range ( 2 , n ) : i f n%i ==0: p r i n t " number not i s prime : " , n break ; i f n%i ! = 0 : p r i n t " number i s prime

5

7

9

11

13

15

: " ,n Listing 1: Example to check if a number is prime or not

Simple properties of prime numbers derived over the years can be used to speed up the algorithms 1. There are no even primes greater than 2. 2. Check the divisibility of values of divisor less or equal to square root of number 3. A number is divisible by 3 if sum of digits is divisible by 3 ( 0,3,6,9) 4. A number is divisible by 9 if sum of digits is divisible by 9 ( 0 and 9) 5. divide a number by 9 ,remainder gives the sum of its digits 6. the sum of digits of prime number is 1,2,5,7,8 except 3 7. if the number >10 and last digit 2,4,5,6,8 or 0 then number is not a prime 8. if number is divisible by 3 it is not a prime 9. if the remainder of number divided by 3 is divisible by 7 it is not a prime

4|6

10. if above conditions have not eliminated the number divide it by odd numbers greater than 11,all numbers less than 11 have been tested by earlier conditions
1

3

# −∗− coding : u t f −8 −∗− """ Created on S a t Dec 15 2 3 : 0 0 : 5 3 2012 @author : p i 1 9 4 0 4 """ # ! / usr/bin/python """ " Program t o check i f number i s prime or not Created on S a t Dec 15 1 9 : 5 8 : 0 8 2012 @author : p i 1 9 4 0 4 """ def prime ( n ) : i f ( n >10) : r 1=n%10 r 2=n%9 a1 = [ 2 , 4 , 5 , 6 , 8 , 0 ] i f ( r 1 i n a1 ) : return 0 i f ( r2 in [ 0 , 3 , 6 , 9 ] ) : return 0 i f ( n%3==0) : return 0 r 4 =(n%3) i f ( r4 >10 & r 4 %7==0) : return 0 f o r i i n range ( 1 1 , n , 2 ) : i f n%i ==0: return 0 return 1

5

7

9

11

13

15

17

19

21

23

25

27

29

31

33

35

37

39

41

e l i f ( n<=10) : a2 = [ 0 , 1 , 4 , 6 , 8 , 9 , 1 0 ] i f ( n i n a2 ) : return 0 else : return 1 n=10111 r e t =prime ( n ) i f r e t ==0: p r i n t " number i s " , n , " prime " else : p r i n t " number i s " , n , " prime " # r 2 =23%9 # f o r i i n range ( 2 , n ) : # i f n%i ==0: # p r i n t " number not i s prime : " , n # break ;

43

45

47

49

51

53

5|6

Prime Numbers
55

57

# # i f n%i ! = 0 : # p r i n t " number i s prime

:" ,n Listing 2: Second Example to check if a number is prime or not

6|6

Sign up to vote on this title
UsefulNot useful