Prime numbers print from range 2…100How do I call Objective-C code from Swift?Swift Beta performance: sorting arraysWhy Swift is 100 times slower than C in this image processing test?Swift: print() vs println() vs NSLog()Random array range 1-4 with each number only printed twiceUpdated to Xcode 7.1 (from 6.4) caused UIIMageView animation brokenAssigning Random numbers that don't repeat to the text in an array of labels SwiftHow to get the current day number in current month and yearSwift control flow counting with multiplesReturn substring from attributed string using range in Swift
What is the difference between "Grippe" and "Männergrippe"?
Changing JPEG to RAW to use on Lightroom?
Why can't you reverse the order of the input redirection operator for while loops?
What is Spectral Subtraction for noise reduction?
When, exactly, does the Rogue Scout get to use their Skirmisher ability?
Why is "-ber" the suffix of the last four months of the year?
Gambler coin problem: fair coin and two-headed coin
Can you grapple/shove when affected by the Crown of Madness spell?
What stops you from using fixed income in developing countries?
Talk interpreter
How should i charge 3 lithium ion batteries?
How long do you think advanced cybernetic implants would plausibly last?
If the Shillelagh cantrip is applied to a club with non-standard damage dice, what is the resulting damage dice?
Redacting URLs as an email-phishing preventative?
How would a low-tech device be able to alert its user?
Can I get a PhD for developing an educational software?
Does Yeshayahu 43:10b / 43:13a imply HaShem was created?
What to look for in a spotting scope?
Billiard balls collision
Where does learning new skills fit into Agile?
Is it legal for source code containing undefined behavior to crash the compiler?
How many birds in the bush?
How do I make my image comply with the requirements of this photography competition?
"There were either twelve sexes or none."
Prime numbers print from range 2…100
How do I call Objective-C code from Swift?Swift Beta performance: sorting arraysWhy Swift is 100 times slower than C in this image processing test?Swift: print() vs println() vs NSLog()Random array range 1-4 with each number only printed twiceUpdated to Xcode 7.1 (from 6.4) caused UIIMageView animation brokenAssigning Random numbers that don't repeat to the text in an array of labels SwiftHow to get the current day number in current month and yearSwift control flow counting with multiplesReturn substring from attributed string using range in Swift
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have been assigned with a task to print prime numbers from a range 2...100. I've managed to get most of the prime numbers but can't figure out how to get rid of 9 and 15, basically multiples of 3 and 5. Please give me your suggestion on how can I fix this.
for n in 2...20
if n % 2 == 0 && n < 3
print(n)
else if n % 2 == 1
print(n)
else if n % 3 == 0 && n > 6
This what it prints so far:
2
3
5
7
9
11
13
15
17
19
swift
add a comment |
I have been assigned with a task to print prime numbers from a range 2...100. I've managed to get most of the prime numbers but can't figure out how to get rid of 9 and 15, basically multiples of 3 and 5. Please give me your suggestion on how can I fix this.
for n in 2...20
if n % 2 == 0 && n < 3
print(n)
else if n % 2 == 1
print(n)
else if n % 3 == 0 && n > 6
This what it prints so far:
2
3
5
7
9
11
13
15
17
19
swift
You should google first. Check this out - en.wikipedia.org/wiki/Primality_test and en.wikipedia.org/wiki/Generating_primes
– battlmonstr
Mar 27 at 19:48
3
is this a homework for university? maybe try solving this by implementing Sieve of Eratosthenes. This algorithm will give you primes below N and if you are having trouble with that, feel free to ask again.
– user3469811
Mar 27 at 19:48
2
You need to handlen % 5 == 0 || n % 3 == 0as well. You are also not guaranteeing that a number is not divisible by another prime, so your test might not give reliable results - search for Sieve of Eratosthenes.
– jweyrich
Mar 27 at 19:49
1
This is pretty well-travelled territory, especially now that Swift has been around for a bit. Have you taken a look at LeetCode solutions?
– Adrian
Mar 27 at 19:52
Have a look here codereview.stackexchange.com/questions/211437/…
– ielyamani
Mar 27 at 21:52
add a comment |
I have been assigned with a task to print prime numbers from a range 2...100. I've managed to get most of the prime numbers but can't figure out how to get rid of 9 and 15, basically multiples of 3 and 5. Please give me your suggestion on how can I fix this.
for n in 2...20
if n % 2 == 0 && n < 3
print(n)
else if n % 2 == 1
print(n)
else if n % 3 == 0 && n > 6
This what it prints so far:
2
3
5
7
9
11
13
15
17
19
swift
I have been assigned with a task to print prime numbers from a range 2...100. I've managed to get most of the prime numbers but can't figure out how to get rid of 9 and 15, basically multiples of 3 and 5. Please give me your suggestion on how can I fix this.
for n in 2...20
if n % 2 == 0 && n < 3
print(n)
else if n % 2 == 1
print(n)
else if n % 3 == 0 && n > 6
This what it prints so far:
2
3
5
7
9
11
13
15
17
19
swift
swift
edited Mar 27 at 19:44
rmaddy
259k29 gold badges351 silver badges416 bronze badges
259k29 gold badges351 silver badges416 bronze badges
asked Mar 27 at 19:41
Sergey SSergey S
12 bronze badges
12 bronze badges
You should google first. Check this out - en.wikipedia.org/wiki/Primality_test and en.wikipedia.org/wiki/Generating_primes
– battlmonstr
Mar 27 at 19:48
3
is this a homework for university? maybe try solving this by implementing Sieve of Eratosthenes. This algorithm will give you primes below N and if you are having trouble with that, feel free to ask again.
– user3469811
Mar 27 at 19:48
2
You need to handlen % 5 == 0 || n % 3 == 0as well. You are also not guaranteeing that a number is not divisible by another prime, so your test might not give reliable results - search for Sieve of Eratosthenes.
– jweyrich
Mar 27 at 19:49
1
This is pretty well-travelled territory, especially now that Swift has been around for a bit. Have you taken a look at LeetCode solutions?
– Adrian
Mar 27 at 19:52
Have a look here codereview.stackexchange.com/questions/211437/…
– ielyamani
Mar 27 at 21:52
add a comment |
You should google first. Check this out - en.wikipedia.org/wiki/Primality_test and en.wikipedia.org/wiki/Generating_primes
– battlmonstr
Mar 27 at 19:48
3
is this a homework for university? maybe try solving this by implementing Sieve of Eratosthenes. This algorithm will give you primes below N and if you are having trouble with that, feel free to ask again.
– user3469811
Mar 27 at 19:48
2
You need to handlen % 5 == 0 || n % 3 == 0as well. You are also not guaranteeing that a number is not divisible by another prime, so your test might not give reliable results - search for Sieve of Eratosthenes.
– jweyrich
Mar 27 at 19:49
1
This is pretty well-travelled territory, especially now that Swift has been around for a bit. Have you taken a look at LeetCode solutions?
– Adrian
Mar 27 at 19:52
Have a look here codereview.stackexchange.com/questions/211437/…
– ielyamani
Mar 27 at 21:52
You should google first. Check this out - en.wikipedia.org/wiki/Primality_test and en.wikipedia.org/wiki/Generating_primes
– battlmonstr
Mar 27 at 19:48
You should google first. Check this out - en.wikipedia.org/wiki/Primality_test and en.wikipedia.org/wiki/Generating_primes
– battlmonstr
Mar 27 at 19:48
3
3
is this a homework for university? maybe try solving this by implementing Sieve of Eratosthenes. This algorithm will give you primes below N and if you are having trouble with that, feel free to ask again.
– user3469811
Mar 27 at 19:48
is this a homework for university? maybe try solving this by implementing Sieve of Eratosthenes. This algorithm will give you primes below N and if you are having trouble with that, feel free to ask again.
– user3469811
Mar 27 at 19:48
2
2
You need to handle
n % 5 == 0 || n % 3 == 0 as well. You are also not guaranteeing that a number is not divisible by another prime, so your test might not give reliable results - search for Sieve of Eratosthenes.– jweyrich
Mar 27 at 19:49
You need to handle
n % 5 == 0 || n % 3 == 0 as well. You are also not guaranteeing that a number is not divisible by another prime, so your test might not give reliable results - search for Sieve of Eratosthenes.– jweyrich
Mar 27 at 19:49
1
1
This is pretty well-travelled territory, especially now that Swift has been around for a bit. Have you taken a look at LeetCode solutions?
– Adrian
Mar 27 at 19:52
This is pretty well-travelled territory, especially now that Swift has been around for a bit. Have you taken a look at LeetCode solutions?
– Adrian
Mar 27 at 19:52
Have a look here codereview.stackexchange.com/questions/211437/…
– ielyamani
Mar 27 at 21:52
Have a look here codereview.stackexchange.com/questions/211437/…
– ielyamani
Mar 27 at 21:52
add a comment |
3 Answers
3
active
oldest
votes
Your "algorithm" is wrong. You should store somewhere your prime numbers and then just check if n satisfies the condition n % prime != 0 for each prime. If it does, just append this n to an array of primes and continue to next n.
Since Swift 4.2 you can use this very simple algorithm using allSatisfy
var primes = [Int]()
for n in 2...100
if primes.allSatisfy( n % $0 != 0 )
primes.append(n)
print(primes) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Or, you can implement more effective Sieve of Eratosthenes algorithm.
Below is my simple implementation (there are ofc more effective solutions)
var range = [Int](2...100)
var p = 0
while true
guard let newPrimeIndex = range.firstIndex(where: $0 > p ) else break
p = range[newPrimeIndex]
range.removeAll(where: $0 % p == 0 && $0 != p )
print(range) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
add a comment |
Made for Swift 5
Unlike other answers, this is easily scaleable. I did not include a pre-made list of prime numbers, which would not work for very large numbers which have not been included.
Here is the algorithm I came up with. I made it as efficient as I can, with the factors only going up to the floor of the square root of the number. This reduces wasted time comparing factors which will never work.
import Foundation
// Function to determine whether a number is prime
func isPrime(_ num: Int) -> Bool
let max = Int(floor(sqrt(Double(num))))
guard max >= 2 else return true
for factor in 2...max
if num.isMultiple(of: factor) // Write num % factor == 0 for older versions of Swift
return false
return true
// Find if a number is prime for numbers 2 to 200
for i in 2...100
if isPrime(i)
print(i)
3
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
add a comment |
I finally figured it out lol, It might be not pretty but it works haha, Thanks for everyone's answer. I'll post what I came up with if maybe it will help anyone else.
for n in 2...100
if n % 2 == 0 && n < 3
print(n)
else if n % 3 == 0 && n > 6
else if n % 5 == 0 && n > 5
else if n % 7 == 0 && n > 7
else if n % 2 == 1
print(n)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385273%2fprime-numbers-print-from-range-2-100%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your "algorithm" is wrong. You should store somewhere your prime numbers and then just check if n satisfies the condition n % prime != 0 for each prime. If it does, just append this n to an array of primes and continue to next n.
Since Swift 4.2 you can use this very simple algorithm using allSatisfy
var primes = [Int]()
for n in 2...100
if primes.allSatisfy( n % $0 != 0 )
primes.append(n)
print(primes) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Or, you can implement more effective Sieve of Eratosthenes algorithm.
Below is my simple implementation (there are ofc more effective solutions)
var range = [Int](2...100)
var p = 0
while true
guard let newPrimeIndex = range.firstIndex(where: $0 > p ) else break
p = range[newPrimeIndex]
range.removeAll(where: $0 % p == 0 && $0 != p )
print(range) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
add a comment |
Your "algorithm" is wrong. You should store somewhere your prime numbers and then just check if n satisfies the condition n % prime != 0 for each prime. If it does, just append this n to an array of primes and continue to next n.
Since Swift 4.2 you can use this very simple algorithm using allSatisfy
var primes = [Int]()
for n in 2...100
if primes.allSatisfy( n % $0 != 0 )
primes.append(n)
print(primes) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Or, you can implement more effective Sieve of Eratosthenes algorithm.
Below is my simple implementation (there are ofc more effective solutions)
var range = [Int](2...100)
var p = 0
while true
guard let newPrimeIndex = range.firstIndex(where: $0 > p ) else break
p = range[newPrimeIndex]
range.removeAll(where: $0 % p == 0 && $0 != p )
print(range) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
add a comment |
Your "algorithm" is wrong. You should store somewhere your prime numbers and then just check if n satisfies the condition n % prime != 0 for each prime. If it does, just append this n to an array of primes and continue to next n.
Since Swift 4.2 you can use this very simple algorithm using allSatisfy
var primes = [Int]()
for n in 2...100
if primes.allSatisfy( n % $0 != 0 )
primes.append(n)
print(primes) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Or, you can implement more effective Sieve of Eratosthenes algorithm.
Below is my simple implementation (there are ofc more effective solutions)
var range = [Int](2...100)
var p = 0
while true
guard let newPrimeIndex = range.firstIndex(where: $0 > p ) else break
p = range[newPrimeIndex]
range.removeAll(where: $0 % p == 0 && $0 != p )
print(range) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Your "algorithm" is wrong. You should store somewhere your prime numbers and then just check if n satisfies the condition n % prime != 0 for each prime. If it does, just append this n to an array of primes and continue to next n.
Since Swift 4.2 you can use this very simple algorithm using allSatisfy
var primes = [Int]()
for n in 2...100
if primes.allSatisfy( n % $0 != 0 )
primes.append(n)
print(primes) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Or, you can implement more effective Sieve of Eratosthenes algorithm.
Below is my simple implementation (there are ofc more effective solutions)
var range = [Int](2...100)
var p = 0
while true
guard let newPrimeIndex = range.firstIndex(where: $0 > p ) else break
p = range[newPrimeIndex]
range.removeAll(where: $0 % p == 0 && $0 != p )
print(range) // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
edited Mar 28 at 10:50
answered Mar 27 at 19:51
Robert DreslerRobert Dresler
8,6762 gold badges9 silver badges28 bronze badges
8,6762 gold badges9 silver badges28 bronze badges
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
add a comment |
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
oh man, this seems still complicated for my level, but maybe soon I will get it. Thanks!
– Sergey S
Mar 27 at 20:09
add a comment |
Made for Swift 5
Unlike other answers, this is easily scaleable. I did not include a pre-made list of prime numbers, which would not work for very large numbers which have not been included.
Here is the algorithm I came up with. I made it as efficient as I can, with the factors only going up to the floor of the square root of the number. This reduces wasted time comparing factors which will never work.
import Foundation
// Function to determine whether a number is prime
func isPrime(_ num: Int) -> Bool
let max = Int(floor(sqrt(Double(num))))
guard max >= 2 else return true
for factor in 2...max
if num.isMultiple(of: factor) // Write num % factor == 0 for older versions of Swift
return false
return true
// Find if a number is prime for numbers 2 to 200
for i in 2...100
if isPrime(i)
print(i)
3
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
add a comment |
Made for Swift 5
Unlike other answers, this is easily scaleable. I did not include a pre-made list of prime numbers, which would not work for very large numbers which have not been included.
Here is the algorithm I came up with. I made it as efficient as I can, with the factors only going up to the floor of the square root of the number. This reduces wasted time comparing factors which will never work.
import Foundation
// Function to determine whether a number is prime
func isPrime(_ num: Int) -> Bool
let max = Int(floor(sqrt(Double(num))))
guard max >= 2 else return true
for factor in 2...max
if num.isMultiple(of: factor) // Write num % factor == 0 for older versions of Swift
return false
return true
// Find if a number is prime for numbers 2 to 200
for i in 2...100
if isPrime(i)
print(i)
3
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
add a comment |
Made for Swift 5
Unlike other answers, this is easily scaleable. I did not include a pre-made list of prime numbers, which would not work for very large numbers which have not been included.
Here is the algorithm I came up with. I made it as efficient as I can, with the factors only going up to the floor of the square root of the number. This reduces wasted time comparing factors which will never work.
import Foundation
// Function to determine whether a number is prime
func isPrime(_ num: Int) -> Bool
let max = Int(floor(sqrt(Double(num))))
guard max >= 2 else return true
for factor in 2...max
if num.isMultiple(of: factor) // Write num % factor == 0 for older versions of Swift
return false
return true
// Find if a number is prime for numbers 2 to 200
for i in 2...100
if isPrime(i)
print(i)
Made for Swift 5
Unlike other answers, this is easily scaleable. I did not include a pre-made list of prime numbers, which would not work for very large numbers which have not been included.
Here is the algorithm I came up with. I made it as efficient as I can, with the factors only going up to the floor of the square root of the number. This reduces wasted time comparing factors which will never work.
import Foundation
// Function to determine whether a number is prime
func isPrime(_ num: Int) -> Bool
let max = Int(floor(sqrt(Double(num))))
guard max >= 2 else return true
for factor in 2...max
if num.isMultiple(of: factor) // Write num % factor == 0 for older versions of Swift
return false
return true
// Find if a number is prime for numbers 2 to 200
for i in 2...100
if isPrime(i)
print(i)
answered Mar 27 at 20:35
George_EGeorge_E
1,6172 gold badges10 silver badges29 bronze badges
1,6172 gold badges10 silver badges29 bronze badges
3
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
add a comment |
3
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
3
3
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
For prime numbers in a large range a sieving method would scale better than this trial division method.
– Martin R
Mar 27 at 20:39
add a comment |
I finally figured it out lol, It might be not pretty but it works haha, Thanks for everyone's answer. I'll post what I came up with if maybe it will help anyone else.
for n in 2...100
if n % 2 == 0 && n < 3
print(n)
else if n % 3 == 0 && n > 6
else if n % 5 == 0 && n > 5
else if n % 7 == 0 && n > 7
else if n % 2 == 1
print(n)
add a comment |
I finally figured it out lol, It might be not pretty but it works haha, Thanks for everyone's answer. I'll post what I came up with if maybe it will help anyone else.
for n in 2...100
if n % 2 == 0 && n < 3
print(n)
else if n % 3 == 0 && n > 6
else if n % 5 == 0 && n > 5
else if n % 7 == 0 && n > 7
else if n % 2 == 1
print(n)
add a comment |
I finally figured it out lol, It might be not pretty but it works haha, Thanks for everyone's answer. I'll post what I came up with if maybe it will help anyone else.
for n in 2...100
if n % 2 == 0 && n < 3
print(n)
else if n % 3 == 0 && n > 6
else if n % 5 == 0 && n > 5
else if n % 7 == 0 && n > 7
else if n % 2 == 1
print(n)
I finally figured it out lol, It might be not pretty but it works haha, Thanks for everyone's answer. I'll post what I came up with if maybe it will help anyone else.
for n in 2...100
if n % 2 == 0 && n < 3
print(n)
else if n % 3 == 0 && n > 6
else if n % 5 == 0 && n > 5
else if n % 7 == 0 && n > 7
else if n % 2 == 1
print(n)
answered Mar 27 at 20:07
Sergey SSergey S
12 bronze badges
12 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385273%2fprime-numbers-print-from-range-2-100%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should google first. Check this out - en.wikipedia.org/wiki/Primality_test and en.wikipedia.org/wiki/Generating_primes
– battlmonstr
Mar 27 at 19:48
3
is this a homework for university? maybe try solving this by implementing Sieve of Eratosthenes. This algorithm will give you primes below N and if you are having trouble with that, feel free to ask again.
– user3469811
Mar 27 at 19:48
2
You need to handle
n % 5 == 0 || n % 3 == 0as well. You are also not guaranteeing that a number is not divisible by another prime, so your test might not give reliable results - search for Sieve of Eratosthenes.– jweyrich
Mar 27 at 19:49
1
This is pretty well-travelled territory, especially now that Swift has been around for a bit. Have you taken a look at LeetCode solutions?
– Adrian
Mar 27 at 19:52
Have a look here codereview.stackexchange.com/questions/211437/…
– ielyamani
Mar 27 at 21:52