Algorithm for checking the prime number or not
(Extend the same to generate 2000 prime numbers)
According to mathematics,
the maximum checks are in the range 2 to number/2 with a increment of 1 for
every check –
LOGIC
According to logic,
Step 1: Declare the three variables, use one variable as flag,
another two to carry out ‘for’ loop
Step 2: Write a for loop with conditions, start loop from 2 and
execute till number/2 with a increment of 1 (I = 2; I<=number/2; I++), set
flag equals to 0.
Step 3: Find the remainder of a number starting from 2 and check for
other number till number/2
Example: if the checking number is
1000 the starting number is 2 to 500
(number/2)
If remainder is equal to zero set the flag = 1 and
terminate the loop
Repeat the step 3 till the loop
count reaches the number/2.
Step 4: Print the results when flag ==0.
Flag ==1 not a prime number
https://youtu.be/eW2gQHXmkCU
No comments:
Post a Comment