C
C#3mo ago
MH

✅ 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
Jimmacle
Jimmacle3mo ago
what do you have so far, where exactly are you stuck?
MH
MHOP3mo ago
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(); } } }
Jimmacle
Jimmacle3mo ago
$code
MODiX
MODiX3mo ago
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/
MH
MHOP3mo ago
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();
}
}
}
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();
}
}
}
the Console.WriteLine(n); is for checking and it is not giving the results needed
Jimmacle
Jimmacle3mo ago
what results are you expecting and what are you getting instead?
MH
MHOP3mo ago
I'm expecting 20 prime numbers
Jimmacle
Jimmacle3mo ago
identify the first place the program is going wrong and work backwards to find the bug
MH
MHOP3mo ago
I can't I've tried too many times
Jimmacle
Jimmacle3mo ago
well, you'll have to to fix the problem what are you getting instead of 20 prime numbers?
MH
MHOP3mo ago
the number 1
Jimmacle
Jimmacle3mo ago
and no other output?
MH
MHOP3mo ago
nope
Jimmacle
Jimmacle3mo ago
what's the purpose of the breaks?
MH
MHOP3mo ago
I think they return the loop to the start I'm not sure
Jimmacle
Jimmacle3mo ago
no they exit the loop completely
MH
MHOP3mo ago
oh
Jimmacle
Jimmacle3mo ago
none of your inner loops will run more than once
MH
MHOP3mo ago
well now that I have deleted the breaks it is just giving me the number 2
Jimmacle
Jimmacle3mo ago
which means you have more bugs try writing the process that you need the code to follow out in plain english
MH
MHOP3mo ago
ok generate 20 prime numbers and then print them
Jimmacle
Jimmacle3mo ago
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
MH
MHOP3mo ago
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
Jimmacle
Jimmacle3mo ago
okay, and what do you need to do to check if a number is prime?
MH
MHOP3mo ago
//
for (x = 1; x <= n; x++)
{
if (n % x == 0)
{
div++;
}
if (div == 2)
{
Console.WriteLine(n);
}
//
for (x = 1; x <= n; x++)
{
if (n % x == 0)
{
div++;
}
if (div == 2)
{
Console.WriteLine(n);
}
Jimmacle
Jimmacle3mo ago
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?
MH
MHOP3mo ago
nothing because this code is only supposed to print primes
Jimmacle
Jimmacle3mo ago
i didn't say tell me what you think it does, actually work through it because it prints something
MH
MHOP3mo ago
ok
Jimmacle
Jimmacle3mo ago
your assumption about what this code does is wrong so you need to check it
MH
MHOP3mo ago
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
Jimmacle
Jimmacle3mo ago
not in that order, no each loop happens one after the other
MH
MHOP3mo ago
is it correct now?
Jimmacle
Jimmacle3mo ago
no it doesn't generate numbers first
MH
MHOP3mo ago
what??
Jimmacle
Jimmacle3mo ago
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
MH
MHOP3mo ago
oh ok
Jimmacle
Jimmacle3mo ago
this is important because it's part of the bug so each time it loops, what's happening in the loop?
MH
MHOP3mo ago
x++ and then check if n % x == 0
Jimmacle
Jimmacle3mo ago
the x++ happens at the very end so the first thing it does is check if n % x == 0 then what?
MH
MHOP3mo ago
ok then div++
Jimmacle
Jimmacle3mo ago
yes, then what after that?
MH
MHOP3mo ago
so now div = 1 after that it skips if(div == 2) and the loop resets
Jimmacle
Jimmacle3mo ago
yes, then let's go through the next iteration of the loop x is 2 and div is still 1, so what happens now?
MH
MHOP3mo ago
x++ so now x is 2
Jimmacle
Jimmacle3mo ago
again, that happens at the end of the loop not the beginning
MH
MHOP3mo ago
yeah I should have said it before the loop resets so now it checks if (n % 2 == 0) so 6 % 2 and then
Jimmacle
Jimmacle3mo ago
did you see it? 😛
MH
MHOP3mo ago
sorry my internet cut out
Jimmacle
Jimmacle3mo ago
well we're almost at the bug
MH
MHOP3mo ago
so it eventually reaches 6 % 6 right?
Jimmacle
Jimmacle3mo ago
let's not skip ahead
MH
MHOP3mo ago
ok
Jimmacle
Jimmacle3mo ago
so it checks 6 % 2 == 0
MH
MHOP3mo ago
then x++
Jimmacle
Jimmacle3mo ago
no is 6 % 2 == 0 true or false?
MH
MHOP3mo ago
true oh damn
Jimmacle
Jimmacle3mo ago
right so we div++ again now div is 2, so what happens with the next if?
MH
MHOP3mo ago
it prints 6
Jimmacle
Jimmacle3mo ago
yep which you don't want since it's not prime
MH
MHOP3mo ago
yeah
Jimmacle
Jimmacle3mo ago
so how would you change the code so that you count up all of the factors before checking how many there are?
MH
MHOP3mo ago
I don't know
Jimmacle
Jimmacle3mo ago
give it some thought
MH
MHOP3mo ago
should I make it for (x = 1; x < n; x++) x < n
Jimmacle
Jimmacle3mo ago
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?
MH
MHOP3mo ago
so I should move if(div == 2) out of this loop right?
Jimmacle
Jimmacle3mo ago
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)
MH
MHOP3mo ago
right so I just did that and now... i'll just send it
//
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x;

for (i = 0; i <= 20; i++)
{
for (n = 6; n <= 500; n++)
{
for (x = 1; x < n; x++)
{
if (n % x == 0)
{
div++;
}

}
if (div == 2)
{
Console.WriteLine(n);
}


}

}
Console.ReadKey();
}
}
}
//
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x;

for (i = 0; i <= 20; i++)
{
for (n = 6; n <= 500; n++)
{
for (x = 1; x < n; x++)
{
if (n % x == 0)
{
div++;
}

}
if (div == 2)
{
Console.WriteLine(n);
}


}

}
Console.ReadKey();
}
}
}
it give me 6 and 7
Jimmacle
Jimmacle3mo ago
you changed the initial values of n and x from what they should be in the final program
MH
MHOP3mo ago
yeah sorry I changed it to 1 and now it gives me nothing
Jimmacle
Jimmacle3mo ago
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
MH
MHOP3mo ago
//
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);
}


}

}
Console.ReadKey();
}
}
}
//
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);
}


}

}
Console.ReadKey();
}
}
}
still gives me nothing
Jimmacle
Jimmacle3mo ago
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?
MH
MHOP3mo ago
0
Jimmacle
Jimmacle3mo ago
does that happen for every number that this code checks?
MH
MHOP3mo ago
nope
Jimmacle
Jimmacle3mo ago
right, we have to reset it each time
MH
MHOP3mo ago
// static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x;

for (i = 1; 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);
}
else
{
div = 0;
}
}

}
Console.ReadKey();
}
}
}
// static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x;

for (i = 1; 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);
}
else
{
div = 0;
}
}

}
Console.ReadKey();
}
}
}
it workedddd kinda
Jimmacle
Jimmacle3mo ago
right why are you only resetting it if it's not 2?
MH
MHOP3mo ago
should I remove the else?
Jimmacle
Jimmacle3mo ago
yep because you want it to restart from 0 no matter what
MH
MHOP3mo ago
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?
Jimmacle
Jimmacle3mo ago
well we only want the first 20 prime numbers right? we should fix that first
MH
MHOP3mo ago
how
Jimmacle
Jimmacle3mo ago
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 here
MH
MHOP3mo ago
oh right but where?
Jimmacle
Jimmacle3mo ago
which loop needs to stop after getting to 20 prime numbers?
MH
MHOP3mo ago
X? the one that generates X? no no N
Jimmacle
Jimmacle3mo ago
yep
MH
MHOP3mo ago
so we wont get anymore Ns
Jimmacle
Jimmacle3mo ago
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
MH
MHOP3mo ago
should I add another variable?
Jimmacle
Jimmacle3mo ago
that's on the right track (brb)
MH
MHOP3mo ago
ok
//
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x, y=0;

for (i = 1; 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);
y++;
}
div = 0;
if (y == 20)
{
break;
}
}

}
Console.ReadKey();
}
}
}
//
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x, y=0;

for (i = 1; 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);
y++;
}
div = 0;
if (y == 20)
{
break;
}
}

}
Console.ReadKey();
}
}
}
Jimmacle
Jimmacle3mo ago
what does the output look like now?
MH
MHOP3mo ago
now it only prints 20 primes nice
Jimmacle
Jimmacle3mo ago
nice, you can also get rid of the whole for (i = 1; i <= 20; i++) loop since it's not doing anything useful now
MH
MHOP3mo ago
//
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x, y=0;

for (i = 1; i <= 20; i++)
{
for (n = 1; n <= 500; n++)
{
for (x = 1; x <= n; x++)
{
if (n % x == 0)
{
div++;
}
}
if (div == 2)
{
sum=sum+n;
y++;
}
div = 0;
if (y == 20)
{
break;
}
}

}
Console.WriteLine("prime sum:" + sum);
Console.ReadKey();
}
}
}
//
static void Main(string[] args)
{
int n = 1, i = 0, div = 0, sum = 0, x, y=0;

for (i = 1; i <= 20; i++)
{
for (n = 1; n <= 500; n++)
{
for (x = 1; x <= n; x++)
{
if (n % x == 0)
{
div++;
}
}
if (div == 2)
{
sum=sum+n;
y++;
}
div = 0;
if (y == 20)
{
break;
}
}

}
Console.WriteLine("prime sum:" + sum);
Console.ReadKey();
}
}
}
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
Jimmacle
Jimmacle3mo ago
:pepeOK: $close
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server