✅ stuck on getting 20 prime numbers C#
I have been trying to figure this out for 4 hours, I just can't. help me please.
so I have to generate 20 prime numbers using for() and then sum them
101 Replies
what do you have so far, where exactly are you stuck?
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x;
for (i = 0; i <= 20; i++)
{
for (n = 1; n <= 500; n++)
{
for (x = 1; x <= n; x++)
{
if (n % x == 0)
{
div++;
}
if (div == 2)
{
Console.WriteLine(n);
}
break;
}
break;
}
}
Console.ReadKey();
}
}
}
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/
the Console.WriteLine(n); is for checking
and it is not giving the results needed
what results are you expecting and what are you getting instead?
I'm expecting 20 prime numbers
identify the first place the program is going wrong and work backwards to find the bug
I can't
I've tried too many times
well, you'll have to to fix the problem
what are you getting instead of 20 prime numbers?
the number 1
and no other output?
nope
what's the purpose of the
break
s?I think they return the loop to the start
I'm not sure
no
they exit the loop completely
oh
none of your inner loops will run more than once
well now that I have deleted the breaks it is just giving me the number 2
which means you have more bugs
try writing the process that you need the code to follow out in plain english
ok
generate 20 prime numbers and then print them
and what steps do you need to follow to do that?
you need to break the problem down into simple steps that you can translate into code
repeat the following line 20 times
generate from 1 to 500 and as it is being generated check which numbers are prime and print the prime numbers
okay, and what do you need to do to check if a number is prime?
let's start with this piece, because it has an issue
pretend n is 6 and work through what the code is doing in your head (or on paper)
what gets printed out?
nothing
because this code is only supposed to print primes
i didn't say tell me what you think it does, actually work through it
because it prints something
ok
your assumption about what this code does is wrong so you need to check it
it first generates X which is 1 2 3 4 5 6 and then divides N with those then checks if N remains are 0 or not
not in that order, no
each loop happens one after the other
is it correct now?
no
it doesn't generate numbers first
what??
it just starts at 1, runs the code inside the loop, then increments x, then runs the code inside the loop, etc until the condition x <= n is no longer true
oh ok
this is important because it's part of the bug
so each time it loops, what's happening in the loop?
x++
and then check if n % x == 0
the x++ happens at the very end
so the first thing it does is check if n % x == 0
then what?
ok
then div++
yes, then what after that?
so now div = 1
after that it skips if(div == 2)
and the loop resets
yes, then let's go through the next iteration of the loop
x is 2 and div is still 1, so what happens now?
x++
so now x is 2
again, that happens at the end of the loop
not the beginning
yeah I should have said it before the loop resets
so now
it checks if (n % 2 == 0)
so 6 % 2
and then
did you see it? 😛
sorry my internet cut out
well we're almost at the bug
so it eventually reaches 6 % 6 right?
let's not skip ahead
ok
so it checks 6 % 2 == 0
then x++
no
is 6 % 2 == 0 true or false?
true
oh damn
right so we div++ again
now div is 2, so what happens with the next if?
it prints 6
yep
which you don't want since it's not prime
yeah
so how would you change the code so that you count up all of the factors before checking how many there are?
I don't know
give it some thought
should I make it
for (x = 1; x < n; x++)
x < n
no, i'll give you a hint
you just have to move some code
think about when you want to check what the value of
div
is
do you need to check it every time you divide or just once at the end?so I should move if(div == 2) out of this loop right?
exactly
you don't want to check it before you've counted up all of the factors, otherwise you'll get false positives every time a number has at least 2 factors (which is nearly every number)
right
so I just did that and now...
i'll just send it
it give me 6 and 7
you changed the initial values of n and x from what they should be in the final program
yeah sorry
I changed it to 1 and now it gives me nothing
you also changed
x <= n
to x < n
to find out if a number is prime you need to divide it by itself, so that change breaks the code
still gives me nothing
yeah, there are still more issues to solve
so in a vacuum the prime code works fine, but here we're missing something
what value should
div
start at when we're checking if a number is prime?0
does that happen for every number that this code checks?
nope
right, we have to reset it each time
it workedddd
kinda
right
why are you only resetting it if it's not 2?
should I remove the else?
yep
because you want it to restart from 0 no matter what
now I am getting every prime number from 1 to 500
20 time over
I think I should remove the print(n)
and sum the prime numbers right?
well we only want the first 20 prime numbers right? we should fix that first
how
well, the code currently prints all the prime numbers from 1 to 500 20 times
but you probably only want to print them once, and stop early once you reach 20
using one of those
break;
s might be useful hereoh right
but where?
which loop needs to stop after getting to 20 prime numbers?
X?
the one that generates X?
no no
N
yep
so we wont get anymore Ns
the one with X is the prime checking code that we know works so we don't have to touch it anymore
so you'll probably need to add a way to keep track of how many prime numbers you've found
should I add another variable?
that's on the right track
(brb)
ok
what does the output look like now?
now it only prints 20 primes
nice
nice, you can also get rid of the whole
for (i = 1; i <= 20; i++)
loop since it's not doing anything useful now
prime sum:639
I got rid of this too
for (i = 1; i <= 20; i++)
thanks
you are too good of a teacher
thank you so much
:pepeOK:
$close
If you have no further questions, please use /close to mark the forum thread as answered