+ 2
How we get prime numbes?
How we write code of prime numbers in swif language and input is user specific in this code?
3 odpowiedzi
+ 5
a prime number is one which has two factors :
1 and itself
so to check you have to see all numbers i from 2 to the number you want to check
if num%i == 0 then not prime
edit : see Abbas Ali's answer
+ 5
@Abdur-Rahmaan I might disagree. To check for a prime number, we might just need to check up to half the value of number-to-be-checked. The reason being, there won't be any factors of a number after it's half. For example:
if 11 needs to be checked, loop needs to run up to 11/2 which is 5 (considering it to be integer value, of course)
After 5, there would naturally be no factors of 11.
So, with this thought we can cut down the number of iterations to half and eventually improving the time complexity.
please correct if I'm wrong :)
+ 5
++ @Abbas Ali