PROBLEM SOLVING TECHNIQUES (15UCAM101) DEPARTMENT OF COMPUTER APPLICATIONS 2018 - 2021
Problem:
Generating all the primes in the first n positive Integers.
Algorithm Development:
A prime Number is a positive integer that is exactly divisible only by 1 and Itself.
Ex:
2,3,5,7,11,13,17,19,23,29,31,37.
All the primes apart from 2 are odd.
For example to test 13 is prime
we divide 13 by 2,3,4,5,6,7,8,9,10,11,12.
If any the above number divides 13 , it is not prime.
In order to generate odd sequence the following is the formula
X= 1
X = x + 2
The following sequence generated
3,5,7,9,11,13,15,17, ………
So for we eliminate the numbers divisible by 2.
next we eliminate the numbers divisible by 3 and 5 so on.
First in the odd sequence with multiples of 3 is removed
In order generate the above pattern
We use the following formula
dx = abs(dx-6) initially dx = 4
To check x is prime or not we need not divide beyond the square root of x.
The following is the algorithm for generate prime numbers to n integers.
C Program:
void main()