![]() the number itself.Īlso Read: C Program for Bubble Sort to Sort Elements in An Order C Program for Prime Numbers Using While Loop Algorithm to Find Prime Number That one factor can be found in the first half, as you can notice that there is only one factor in the second half and i.e. So, to find if a number is prime or not, finding only one factor is enough. ![]() 6, there is only one factor left that is the number itself (12 in this case). You can observe here that after 12/2 i.e. What’s the reason for leaving the other half? Let us understand this with the help of examples. You could ask why we are iterating the loop till n/2 instead of n. The program is not using any extra auxiliary space, only constant space is being used. Pseudocode to Find Prime NumberĪs the loop is iterating from 2 to n/2, the time complexity for the worst case will be O(n), where n is the input element. STEP 4: If num is divisible by loop iterator, then increment temp. STEP 3: Iterate a “for” loop from 2 to num/2. (n - 1) ! ≡ (n-1) mod n C Program for Prime Numbers Using For Loop Algorithm to Find Prime Number
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |