+ 2

How we get prime numbes?

How we write code of prime numbers in swif language and input is user specific in this code?

27th Feb 2018, 4:48 AM
Muhammad Bilal
Muhammad Bilal - avatar
3 Antworten
+ 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
27th Feb 2018, 5:52 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 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 :)
27th Feb 2018, 6:05 AM
Abbas Ali
Abbas Ali - avatar
+ 5
++ @Abbas Ali
27th Feb 2018, 6:07 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar