C
C#2mo 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
Jimmacle2mo ago
what do you have so far, where exactly are you stuck?
MH
MHOP2mo 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
Jimmacle2mo ago
$code
MODiX
MODiX2mo 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
MHOP2mo 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
Jimmacle2mo ago
what results are you expecting and what are you getting instead?
MH
MHOP2mo ago
I'm expecting 20 prime numbers
Jimmacle
Jimmacle2mo ago
identify the first place the program is going wrong and work backwards to find the bug
MH
MHOP2mo ago
I can't I've tried too many times
Jimmacle
Jimmacle2mo ago
well, you'll have to to fix the problem what are you getting instead of 20 prime numbers?
MH
MHOP2mo ago
the number 1
Jimmacle
Jimmacle2mo ago
and no other output?
MH
MHOP2mo ago
nope
Jimmacle
Jimmacle2mo ago
what's the purpose of the breaks?
MH
MHOP2mo ago
I think they return the loop to the start I'm not sure
Jimmacle
Jimmacle2mo ago
no they exit the loop completely
MH
MHOP2mo ago
oh
Jimmacle
Jimmacle2mo ago
none of your inner loops will run more than once
MH
MHOP2mo ago
well now that I have deleted the breaks it is just giving me the number 2
Jimmacle
Jimmacle2mo ago
which means you have more bugs try writing the process that you need the code to follow out in plain english
MH
MHOP2mo ago
ok generate 20 prime numbers and then print them
Jimmacle
Jimmacle2mo 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
MHOP2mo 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
Jimmacle2mo ago
okay, and what do you need to do to check if a number is prime?
MH
MHOP2mo 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
Jimmacle2mo 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
MHOP2mo ago
nothing because this code is only supposed to print primes
Jimmacle
Jimmacle2mo ago
i didn't say tell me what you think it does, actually work through it because it prints something
MH
MHOP2mo ago
ok
Jimmacle
Jimmacle2mo ago
your assumption about what this code does is wrong so you need to check it
MH
MHOP2mo 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
Jimmacle2mo ago
not in that order, no each loop happens one after the other
MH
MHOP2mo ago
is it correct now?
Jimmacle
Jimmacle2mo ago
no it doesn't generate numbers first
MH
MHOP2mo ago
what??
Jimmacle
Jimmacle2mo 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
MHOP2mo ago
oh ok
Jimmacle
Jimmacle2mo ago
this is important because it's part of the bug so each time it loops, what's happening in the loop?
MH
MHOP2mo ago
x++ and then check if n % x == 0
Jimmacle
Jimmacle2mo ago
the x++ happens at the very end so the first thing it does is check if n % x == 0 then what?
MH
MHOP2mo ago
ok then div++
Jimmacle
Jimmacle2mo ago
yes, then what after that?
MH
MHOP2mo ago
so now div = 1 after that it skips if(div == 2) and the loop resets
Jimmacle
Jimmacle2mo 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
MHOP2mo ago
x++ so now x is 2
Jimmacle
Jimmacle2mo ago
again, that happens at the end of the loop not the beginning
MH
MHOP2mo 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
Jimmacle2mo ago
did you see it? 😛
MH
MHOP2mo ago
sorry my internet cut out
Jimmacle
Jimmacle2mo ago
well we're almost at the bug
MH
MHOP2mo ago
so it eventually reaches 6 % 6 right?
Jimmacle
Jimmacle2mo ago
let's not skip ahead
MH
MHOP2mo ago
ok
Jimmacle
Jimmacle2mo ago
so it checks 6 % 2 == 0
MH
MHOP2mo ago
then x++
Jimmacle
Jimmacle2mo ago
no is 6 % 2 == 0 true or false?
MH
MHOP2mo ago
true oh damn
Jimmacle
Jimmacle2mo ago
right so we div++ again now div is 2, so what happens with the next if?
MH
MHOP2mo ago
it prints 6
Jimmacle
Jimmacle2mo ago
yep which you don't want since it's not prime
MH
MHOP2mo ago
yeah
Jimmacle
Jimmacle2mo ago
so how would you change the code so that you count up all of the factors before checking how many there are?
MH
MHOP2mo ago
I don't know
Jimmacle
Jimmacle2mo ago
give it some thought
MH
MHOP2mo ago
should I make it for (x = 1; x < n; x++) x < n
Jimmacle
Jimmacle2mo 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
MHOP2mo ago
so I should move if(div == 2) out of this loop right?
Jimmacle
Jimmacle2mo 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
MHOP2mo 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
Jimmacle2mo ago
you changed the initial values of n and x from what they should be in the final program
MH
MHOP2mo ago
yeah sorry I changed it to 1 and now it gives me nothing
Jimmacle
Jimmacle2mo 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
MHOP2mo 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
Jimmacle2mo 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
MHOP2mo ago
0
Jimmacle
Jimmacle2mo ago
does that happen for every number that this code checks?
MH
MHOP2mo ago
nope
Jimmacle
Jimmacle2mo ago
right, we have to reset it each time
MH
MHOP2mo 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
Jimmacle2mo ago
right why are you only resetting it if it's not 2?
MH
MHOP2mo ago
should I remove the else?
Jimmacle
Jimmacle2mo ago
yep because you want it to restart from 0 no matter what
MH
MHOP2mo 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
Jimmacle2mo ago
well we only want the first 20 prime numbers right? we should fix that first
MH
MHOP2mo ago
how
Jimmacle
Jimmacle2mo 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
MHOP2mo ago
oh right but where?
Jimmacle
Jimmacle2mo ago
which loop needs to stop after getting to 20 prime numbers?
MH
MHOP2mo ago
X? the one that generates X? no no N
Jimmacle
Jimmacle2mo ago
yep
MH
MHOP2mo ago
so we wont get anymore Ns
Jimmacle
Jimmacle2mo 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
MHOP2mo ago
should I add another variable?
Jimmacle
Jimmacle2mo ago
that's on the right track (brb)
MH
MHOP2mo 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
Jimmacle2mo ago
what does the output look like now?
MH
MHOP2mo ago
now it only prints 20 primes nice
Jimmacle
Jimmacle2mo 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
MHOP2mo 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
Jimmacle2mo ago
:pepeOK: $close
MODiX
MODiX2mo 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