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