How to create a loop that returns the first N prime numbers in a vector?How do I loop through or enumerate a JavaScript object?How do I break out of nested loops in Java?How to determine the first and last iteration in a foreach loop?How to break out of jQuery each LoopHow to make a vector using a for loopdata.table vs dplyr: can one do something well the other can't or does poorly?How to recursively compare vectors?why am I getting only 2 and 3 as false? (returns true if number is prime)How else can I use the while-loop with the [included function] to sum only prime numbers from a given range?Print all prime numbers less than 50
Edge style for certain types of nodes in TikZ-Tree
How to ask a man to not take up more than one seat on public transport while avoiding conflict?
Lead Amalgam as a Material for a Sword
What's the purpose of autocorrelation?
Cemented carbide swords - worth it?
Why would a fighter use the afterburner and air brakes at the same time?
Why does Canada require a minimum rate of climb for ultralights of 300 ft/min?
Do household ovens ventilate heat to the outdoors?
Is there an in-universe reason Harry says this or is this simply a Rowling mistake?
Is it safe to put a microwave in a walk-in closet?
How often is duct tape used during crewed space missions?
Minimize taxes now that I earn more
Amiga 500 OCS/ECS vs Mega Drive VDP
What did the controller say during my approach to land (audio clip)?
Did slaves have slaves?
What is the meaning of 上げて落としたみて?
Simulate a 1D Game-of-Life-ish Model
Can I separate garlic into cloves for storage?
Paradox regarding phase transitions in relativistic systems
I feel like most of my characters are the same, what can I do?
Who are the people reviewing far more papers than they're submitting for review?
Is it safe to unplug a blinking USB drive after 'safely' ejecting it?
Fun time! Guess what I am!
Floating Point XOR
How to create a loop that returns the first N prime numbers in a vector?
How do I loop through or enumerate a JavaScript object?How do I break out of nested loops in Java?How to determine the first and last iteration in a foreach loop?How to break out of jQuery each LoopHow to make a vector using a for loopdata.table vs dplyr: can one do something well the other can't or does poorly?How to recursively compare vectors?why am I getting only 2 and 3 as false? (returns true if number is prime)How else can I use the while-loop with the [included function] to sum only prime numbers from a given range?Print all prime numbers less than 50
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
i've been trying for a while now to write a function that has two arguments. The one an integer = n and the other a logical argument. How would i write a function that would return either the first n prime numbers in a vector if the logical argument is true or the n th prime number if the logical argument is false?
This is as far as i have gotten.
getprime <- function(n=0 , all=TRUE)
if (n<=0)
print("Not a valid number")
else if (n>0)
for (primen in 1:n)
while (n %% 2:(n-1) == 0)
n=n+1
print(n)
print(n)
The results that have to be displayed are shown below.
> genprime(7, all=TRUE)
[1] 2 3 5 7 11 13 17
> genprime(7, all=FALSE)
[1] 17
r function loops
|
show 1 more comment
i've been trying for a while now to write a function that has two arguments. The one an integer = n and the other a logical argument. How would i write a function that would return either the first n prime numbers in a vector if the logical argument is true or the n th prime number if the logical argument is false?
This is as far as i have gotten.
getprime <- function(n=0 , all=TRUE)
if (n<=0)
print("Not a valid number")
else if (n>0)
for (primen in 1:n)
while (n %% 2:(n-1) == 0)
n=n+1
print(n)
print(n)
The results that have to be displayed are shown below.
> genprime(7, all=TRUE)
[1] 2 3 5 7 11 13 17
> genprime(7, all=FALSE)
[1] 17
r function loops
Hi , i did forget to add the a function to check for primeness but that isn't where i'm stuck. I just cant seem to create a loop that returns a vector of the first n'th prime numbers. The task also needs to be one function that doesn't use any added packages in R.
– JD kreyfelt
Mar 28 at 14:23
The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function. I already wrote a function to check for primes but i cant use it as everything needs to be done in one function.
– JD kreyfelt
Mar 28 at 14:35
Deleted my comments because the advice was a bit off. Have you thought about the algorithm? How do you want this to work? Let's sayn = 3. What should it do? You need to check if 1 is prime? no. Check if 2 is prime? Yes! -- add 2 to aprimes_vector. How long is the primes vector? only one number in it, 2, but we need n = 3 primes. Better keep going. Check if 3 is prime? Yes! ... etc. The main question is how you want to check for prime-ness, what numbers you need to check if they divide your current prime candidate.
– Gregor
Mar 28 at 14:39
I want to say that "The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function" is a really dumb requirement. Good programming is writing lots of little functions and putting them together. You could include youris_primefunction definition inside this function and still use it.
– Gregor
Mar 28 at 14:41
What you described is exactly what i want to do! But im confused on how to repeat the loop until N primes is reached. I usedany(n %% 2:(n-1) == 0)to check if a number is prime or not but to continue this check and add the primes into a vector is where i am stuck.
– JD kreyfelt
Mar 28 at 14:48
|
show 1 more comment
i've been trying for a while now to write a function that has two arguments. The one an integer = n and the other a logical argument. How would i write a function that would return either the first n prime numbers in a vector if the logical argument is true or the n th prime number if the logical argument is false?
This is as far as i have gotten.
getprime <- function(n=0 , all=TRUE)
if (n<=0)
print("Not a valid number")
else if (n>0)
for (primen in 1:n)
while (n %% 2:(n-1) == 0)
n=n+1
print(n)
print(n)
The results that have to be displayed are shown below.
> genprime(7, all=TRUE)
[1] 2 3 5 7 11 13 17
> genprime(7, all=FALSE)
[1] 17
r function loops
i've been trying for a while now to write a function that has two arguments. The one an integer = n and the other a logical argument. How would i write a function that would return either the first n prime numbers in a vector if the logical argument is true or the n th prime number if the logical argument is false?
This is as far as i have gotten.
getprime <- function(n=0 , all=TRUE)
if (n<=0)
print("Not a valid number")
else if (n>0)
for (primen in 1:n)
while (n %% 2:(n-1) == 0)
n=n+1
print(n)
print(n)
The results that have to be displayed are shown below.
> genprime(7, all=TRUE)
[1] 2 3 5 7 11 13 17
> genprime(7, all=FALSE)
[1] 17
r function loops
r function loops
edited Mar 28 at 14:45
JD kreyfelt
asked Mar 28 at 14:16
JD kreyfeltJD kreyfelt
355 bronze badges
355 bronze badges
Hi , i did forget to add the a function to check for primeness but that isn't where i'm stuck. I just cant seem to create a loop that returns a vector of the first n'th prime numbers. The task also needs to be one function that doesn't use any added packages in R.
– JD kreyfelt
Mar 28 at 14:23
The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function. I already wrote a function to check for primes but i cant use it as everything needs to be done in one function.
– JD kreyfelt
Mar 28 at 14:35
Deleted my comments because the advice was a bit off. Have you thought about the algorithm? How do you want this to work? Let's sayn = 3. What should it do? You need to check if 1 is prime? no. Check if 2 is prime? Yes! -- add 2 to aprimes_vector. How long is the primes vector? only one number in it, 2, but we need n = 3 primes. Better keep going. Check if 3 is prime? Yes! ... etc. The main question is how you want to check for prime-ness, what numbers you need to check if they divide your current prime candidate.
– Gregor
Mar 28 at 14:39
I want to say that "The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function" is a really dumb requirement. Good programming is writing lots of little functions and putting them together. You could include youris_primefunction definition inside this function and still use it.
– Gregor
Mar 28 at 14:41
What you described is exactly what i want to do! But im confused on how to repeat the loop until N primes is reached. I usedany(n %% 2:(n-1) == 0)to check if a number is prime or not but to continue this check and add the primes into a vector is where i am stuck.
– JD kreyfelt
Mar 28 at 14:48
|
show 1 more comment
Hi , i did forget to add the a function to check for primeness but that isn't where i'm stuck. I just cant seem to create a loop that returns a vector of the first n'th prime numbers. The task also needs to be one function that doesn't use any added packages in R.
– JD kreyfelt
Mar 28 at 14:23
The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function. I already wrote a function to check for primes but i cant use it as everything needs to be done in one function.
– JD kreyfelt
Mar 28 at 14:35
Deleted my comments because the advice was a bit off. Have you thought about the algorithm? How do you want this to work? Let's sayn = 3. What should it do? You need to check if 1 is prime? no. Check if 2 is prime? Yes! -- add 2 to aprimes_vector. How long is the primes vector? only one number in it, 2, but we need n = 3 primes. Better keep going. Check if 3 is prime? Yes! ... etc. The main question is how you want to check for prime-ness, what numbers you need to check if they divide your current prime candidate.
– Gregor
Mar 28 at 14:39
I want to say that "The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function" is a really dumb requirement. Good programming is writing lots of little functions and putting them together. You could include youris_primefunction definition inside this function and still use it.
– Gregor
Mar 28 at 14:41
What you described is exactly what i want to do! But im confused on how to repeat the loop until N primes is reached. I usedany(n %% 2:(n-1) == 0)to check if a number is prime or not but to continue this check and add the primes into a vector is where i am stuck.
– JD kreyfelt
Mar 28 at 14:48
Hi , i did forget to add the a function to check for primeness but that isn't where i'm stuck. I just cant seem to create a loop that returns a vector of the first n'th prime numbers. The task also needs to be one function that doesn't use any added packages in R.
– JD kreyfelt
Mar 28 at 14:23
Hi , i did forget to add the a function to check for primeness but that isn't where i'm stuck. I just cant seem to create a loop that returns a vector of the first n'th prime numbers. The task also needs to be one function that doesn't use any added packages in R.
– JD kreyfelt
Mar 28 at 14:23
The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function. I already wrote a function to check for primes but i cant use it as everything needs to be done in one function.
– JD kreyfelt
Mar 28 at 14:35
The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function. I already wrote a function to check for primes but i cant use it as everything needs to be done in one function.
– JD kreyfelt
Mar 28 at 14:35
Deleted my comments because the advice was a bit off. Have you thought about the algorithm? How do you want this to work? Let's say
n = 3. What should it do? You need to check if 1 is prime? no. Check if 2 is prime? Yes! -- add 2 to a primes_vector. How long is the primes vector? only one number in it, 2, but we need n = 3 primes. Better keep going. Check if 3 is prime? Yes! ... etc. The main question is how you want to check for prime-ness, what numbers you need to check if they divide your current prime candidate.– Gregor
Mar 28 at 14:39
Deleted my comments because the advice was a bit off. Have you thought about the algorithm? How do you want this to work? Let's say
n = 3. What should it do? You need to check if 1 is prime? no. Check if 2 is prime? Yes! -- add 2 to a primes_vector. How long is the primes vector? only one number in it, 2, but we need n = 3 primes. Better keep going. Check if 3 is prime? Yes! ... etc. The main question is how you want to check for prime-ness, what numbers you need to check if they divide your current prime candidate.– Gregor
Mar 28 at 14:39
I want to say that "The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function" is a really dumb requirement. Good programming is writing lots of little functions and putting them together. You could include your
is_prime function definition inside this function and still use it.– Gregor
Mar 28 at 14:41
I want to say that "The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function" is a really dumb requirement. Good programming is writing lots of little functions and putting them together. You could include your
is_prime function definition inside this function and still use it.– Gregor
Mar 28 at 14:41
What you described is exactly what i want to do! But im confused on how to repeat the loop until N primes is reached. I used
any(n %% 2:(n-1) == 0) to check if a number is prime or not but to continue this check and add the primes into a vector is where i am stuck.– JD kreyfelt
Mar 28 at 14:48
What you described is exactly what i want to do! But im confused on how to repeat the loop until N primes is reached. I used
any(n %% 2:(n-1) == 0) to check if a number is prime or not but to continue this check and add the primes into a vector is where i am stuck.– JD kreyfelt
Mar 28 at 14:48
|
show 1 more comment
1 Answer
1
active
oldest
votes
You could write a recursive function (a function that calls itself). In addition to the two arguments n and all, it would also take a counter i and a (empty) vector of prime numbers primes.
As long as the number of prime numbers found is less than n, the function calls itself and with each time it calls itself, it would increase the counter variable i. If i is a prime number, it gets added to the primes vector.
get_prime <- function(n, all = TRUE, i = 1, primes = c())
if ( n <= 0)
stop("Not a valid number")
if (length(primes) < n) else
if (all)
return(primes)
else
return(tail(primes, 1))
The results are:
get_prime(7, TRUE)
[1] 2 3 5 7 11 13 17
get_prime(7, FALSE)
[1] 17
1
And, if you want to be fancy, instead of checking2L:ceiling(sqrt(i)), checkprimes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code2as the first prime soprimesis never empty)
– Gregor
Mar 28 at 14:58
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you usei == 2Lwhat does the 2L mean and why did you useceiling(sqrt(i)?
– JD kreyfelt
Mar 28 at 15:19
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
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/4.0/"u003ecc by-sa 4.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%2f55399824%2fhow-to-create-a-loop-that-returns-the-first-n-prime-numbers-in-a-vector%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could write a recursive function (a function that calls itself). In addition to the two arguments n and all, it would also take a counter i and a (empty) vector of prime numbers primes.
As long as the number of prime numbers found is less than n, the function calls itself and with each time it calls itself, it would increase the counter variable i. If i is a prime number, it gets added to the primes vector.
get_prime <- function(n, all = TRUE, i = 1, primes = c())
if ( n <= 0)
stop("Not a valid number")
if (length(primes) < n) else
if (all)
return(primes)
else
return(tail(primes, 1))
The results are:
get_prime(7, TRUE)
[1] 2 3 5 7 11 13 17
get_prime(7, FALSE)
[1] 17
1
And, if you want to be fancy, instead of checking2L:ceiling(sqrt(i)), checkprimes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code2as the first prime soprimesis never empty)
– Gregor
Mar 28 at 14:58
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you usei == 2Lwhat does the 2L mean and why did you useceiling(sqrt(i)?
– JD kreyfelt
Mar 28 at 15:19
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
add a comment
|
You could write a recursive function (a function that calls itself). In addition to the two arguments n and all, it would also take a counter i and a (empty) vector of prime numbers primes.
As long as the number of prime numbers found is less than n, the function calls itself and with each time it calls itself, it would increase the counter variable i. If i is a prime number, it gets added to the primes vector.
get_prime <- function(n, all = TRUE, i = 1, primes = c())
if ( n <= 0)
stop("Not a valid number")
if (length(primes) < n) else
if (all)
return(primes)
else
return(tail(primes, 1))
The results are:
get_prime(7, TRUE)
[1] 2 3 5 7 11 13 17
get_prime(7, FALSE)
[1] 17
1
And, if you want to be fancy, instead of checking2L:ceiling(sqrt(i)), checkprimes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code2as the first prime soprimesis never empty)
– Gregor
Mar 28 at 14:58
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you usei == 2Lwhat does the 2L mean and why did you useceiling(sqrt(i)?
– JD kreyfelt
Mar 28 at 15:19
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
add a comment
|
You could write a recursive function (a function that calls itself). In addition to the two arguments n and all, it would also take a counter i and a (empty) vector of prime numbers primes.
As long as the number of prime numbers found is less than n, the function calls itself and with each time it calls itself, it would increase the counter variable i. If i is a prime number, it gets added to the primes vector.
get_prime <- function(n, all = TRUE, i = 1, primes = c())
if ( n <= 0)
stop("Not a valid number")
if (length(primes) < n) else
if (all)
return(primes)
else
return(tail(primes, 1))
The results are:
get_prime(7, TRUE)
[1] 2 3 5 7 11 13 17
get_prime(7, FALSE)
[1] 17
You could write a recursive function (a function that calls itself). In addition to the two arguments n and all, it would also take a counter i and a (empty) vector of prime numbers primes.
As long as the number of prime numbers found is less than n, the function calls itself and with each time it calls itself, it would increase the counter variable i. If i is a prime number, it gets added to the primes vector.
get_prime <- function(n, all = TRUE, i = 1, primes = c())
if ( n <= 0)
stop("Not a valid number")
if (length(primes) < n) else
if (all)
return(primes)
else
return(tail(primes, 1))
The results are:
get_prime(7, TRUE)
[1] 2 3 5 7 11 13 17
get_prime(7, FALSE)
[1] 17
answered Mar 28 at 14:48
clemensclemens
4,5172 gold badges5 silver badges19 bronze badges
4,5172 gold badges5 silver badges19 bronze badges
1
And, if you want to be fancy, instead of checking2L:ceiling(sqrt(i)), checkprimes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code2as the first prime soprimesis never empty)
– Gregor
Mar 28 at 14:58
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you usei == 2Lwhat does the 2L mean and why did you useceiling(sqrt(i)?
– JD kreyfelt
Mar 28 at 15:19
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
add a comment
|
1
And, if you want to be fancy, instead of checking2L:ceiling(sqrt(i)), checkprimes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code2as the first prime soprimesis never empty)
– Gregor
Mar 28 at 14:58
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you usei == 2Lwhat does the 2L mean and why did you useceiling(sqrt(i)?
– JD kreyfelt
Mar 28 at 15:19
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
1
1
And, if you want to be fancy, instead of checking
2L:ceiling(sqrt(i)), check primes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code 2 as the first prime so primes is never empty)– Gregor
Mar 28 at 14:58
And, if you want to be fancy, instead of checking
2L:ceiling(sqrt(i)), check primes[primes <= ceiling(sqrt(i))]. (would work better if you hard-code 2 as the first prime so primes is never empty)– Gregor
Mar 28 at 14:58
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you use
i == 2L what does the 2L mean and why did you use ceiling(sqrt(i)?– JD kreyfelt
Mar 28 at 15:19
Thank you so much for the assistance. Our lecturer threw us into the deep end on this as we have barely done any R work in our statistics course so ive been struggling quite a bit. Also why did you use
i == 2L what does the 2L mean and why did you use ceiling(sqrt(i)?– JD kreyfelt
Mar 28 at 15:19
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
Would it be possible also to only use 2 arguments in the function as opposed to 4?
– JD kreyfelt
Mar 28 at 15:22
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55399824%2fhow-to-create-a-loop-that-returns-the-first-n-prime-numbers-in-a-vector%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
Hi , i did forget to add the a function to check for primeness but that isn't where i'm stuck. I just cant seem to create a loop that returns a vector of the first n'th prime numbers. The task also needs to be one function that doesn't use any added packages in R.
– JD kreyfelt
Mar 28 at 14:23
The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function. I already wrote a function to check for primes but i cant use it as everything needs to be done in one function.
– JD kreyfelt
Mar 28 at 14:35
Deleted my comments because the advice was a bit off. Have you thought about the algorithm? How do you want this to work? Let's say
n = 3. What should it do? You need to check if 1 is prime? no. Check if 2 is prime? Yes! -- add 2 to aprimes_vector. How long is the primes vector? only one number in it, 2, but we need n = 3 primes. Better keep going. Check if 3 is prime? Yes! ... etc. The main question is how you want to check for prime-ness, what numbers you need to check if they divide your current prime candidate.– Gregor
Mar 28 at 14:39
I want to say that "The whole task has to be done in one function , so i cannot write a seperate function and include it in the new function" is a really dumb requirement. Good programming is writing lots of little functions and putting them together. You could include your
is_primefunction definition inside this function and still use it.– Gregor
Mar 28 at 14:41
What you described is exactly what i want to do! But im confused on how to repeat the loop until N primes is reached. I used
any(n %% 2:(n-1) == 0)to check if a number is prime or not but to continue this check and add the primes into a vector is where i am stuck.– JD kreyfelt
Mar 28 at 14:48