C
C#2y ago
Danyal

✅ This shouldn't work for 2 and 3 but it does

Prime number checker:
static void Plus(int number)
{
Func<int, int> addone = x => (number + 1) / 6;
Func<int, int> minusone = y => (number - 1) / 6;

if (addone(number) % 6 != 0 || minusone(number) % 6 != 0)
{
Console.WriteLine("{0} is not prime", number);
}
else
{
Console.WriteLine("{0} is prime", number);
}
}
static void Plus(int number)
{
Func<int, int> addone = x => (number + 1) / 6;
Func<int, int> minusone = y => (number - 1) / 6;

if (addone(number) % 6 != 0 || minusone(number) % 6 != 0)
{
Console.WriteLine("{0} is not prime", number);
}
else
{
Console.WriteLine("{0} is prime", number);
}
}
29 Replies
Buddy
Buddy2y ago
$details
MODiX
MODiX2y ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Kouhai
Kouhai2y ago
Why do your delegates take parameters, you're discarding them.
Danyal
DanyalOP2y ago
By delegates do you mean the Funcs?
Kouhai
Kouhai2y ago
Yeah
Danyal
DanyalOP2y ago
It told me to write it like that online
Kouhai
Kouhai2y ago
Who told you to write it like that? You can basically think of this code like this
int number = 10
addone(20);
int addone(int x)
{
return (number + 1) / 6
}
int number = 10
addone(20);
int addone(int x)
{
return (number + 1) / 6
}
You're not using the parameter x
Danyal
DanyalOP2y ago
Kouhai
Kouhai2y ago
Now to answer why it fails it's because (3+1/6) = 0 0 % 6 = 0 Yeah, here the parameter is used
Danyal
DanyalOP2y ago
I'm new to lambda that's why my code is bad
Matt
Matt2y ago
why are you using delegates anyways
Danyal
DanyalOP2y ago
because my teacher wanted us to impress him and we hadnt learnt about them
Kouhai
Kouhai2y ago
Well, using lambdas and delegates doesn't make sense tbh It's redundant
Danyal
DanyalOP2y ago
so its not needed?
Kouhai
Kouhai2y ago
Yeah, it's not
Danyal
DanyalOP2y ago
oh so when would it be needed?
Kouhai
Kouhai2y ago
Lambdas or delegates?
Matt
Matt2y ago
I don't think this is a valid prime number checker? Number 17 is prime but that code says it isn't unless I'm missing something lol.
Kouhai
Kouhai2y ago
Yeah, it won't work, it'll fail on 4 for example
Danyal
DanyalOP2y ago
lambdas
Matt
Matt2y ago
(17 + 1) / 6 = 3 which doesn't divide by 6 exactly
Danyal
DanyalOP2y ago
yeah
Kouhai
Kouhai2y ago
you can think of lambdas as basically inline functions
Matt
Matt2y ago
assuming you're doing an Uni course, your teacher most likely wants you to implement a primality test like the ones at https://en.wikipedia.org/wiki/Primality_test
Danyal
DanyalOP2y ago
im in college
Kouhai
Kouhai2y ago
They are called "closures", they can access variables in the the outside scope. In your example, you can see how you access the number parameters x => (number + 1) / 6;
Danyal
DanyalOP2y ago
yeah
Kouhai
Kouhai2y ago
In your case, you don't need to do that at all
Danyal
DanyalOP2y ago
alright thanks for the help!
Want results from more Discord servers?
Add your server