C
C#2y ago
AceChewy

✅ Need help with an if statement

I'm essentially trying to write an if statement where the code executes only if the list named "list" contains only the variables n and the number 1
33 Replies
Moods
Moods2y ago
Does that run??? If the user entered a number bigger than 2 the condition checks for mod 0 Also Convert.ToInt32 isn’t really good
JakenVeina
JakenVeina2y ago
why not?
Moods
Moods2y ago
I could enter abc
JakenVeina
JakenVeina2y ago
oh, right, it's the string version yeah, you want Int32.TryParse()
AceChewy
AceChewyOP2y ago
I'm not sure how to make an if statement where it executes when only two specific values are found within the list a value in the lsit equal to 1 and another equal to number Oh ok, thanks for the heads up what would the function for that last bit be?
Moods
Moods2y ago
Why are you checking if the count is equal to 2 :o Also is this like a function to find all the prime numbers up to a given input?
AceChewy
AceChewyOP2y ago
More so to check if a number is prime
Moods
Moods2y ago
If I entered 2, and I’m reading this correctly, it would check if 2 modulo 0 was equal to 0?
AceChewy
AceChewyOP2y ago
I'm doing it quite inefficiently but essentially, I only want to store the value of i ( the factor) if the remainder is 0 and a priem numbers factor is itself or one, it means if i store them in an array, I can check whether that's true and return an output I thought the % was checking for a remainder
Moods
Moods2y ago
Which it does by dividing no? I may be wrong though
MODiX
MODiX2y ago
calledude
REPL Result: Success
0 % 2
0 % 2
Result: int
0
0
Compile: 265.411ms | Execution: 21.731ms | React with ❌ to remove this embed.
Moods
Moods2y ago
The 0 is on the right hand side Also yeah I could have used modix to check (but idk how to use the bot) Ok yeah in the first loop iteration unless number was less than 2 it’d always check for number % i And i happens to be 0 then
AceChewy
AceChewyOP2y ago
Yh, I just added that piece of code since you can't get a prime number below 2
Moods
Moods2y ago
Wouldn’t the easiest way be, to loop from 2 to half(floor) the input, then running mod on the input with each i to see if it’s prime
AceChewy
AceChewyOP2y ago
Oh yh, that would work - let me try that real quick
AceChewy
AceChewyOP2y ago
Right, so I've rearranged some parts now (still not complete
AceChewy
AceChewyOP2y ago
How do I ensure that the else branch does nothing and just moves onto the next count of x
Moods
Moods2y ago
by not having it Also what is the list doing there
AceChewy
AceChewyOP2y ago
So initally I simply wanted to check if a number is prime and so the list would store the number and the value 1 to show that a number is prime
Moods
Moods2y ago
Ohhhhh
AceChewy
AceChewyOP2y ago
However, I had forgotten that I wanted to return the prime factors, so now it stores those I've made a mess of the explanation so sorry about that Probably could have added some comments
Moods
Moods2y ago
Now I’m just curious as to why there’s a loop inside a loop You can just have one loop
AceChewy
AceChewyOP2y ago
Wait, the first loop is redundant
AceChewy
AceChewyOP2y ago
Ok, I've streamlined some things but now I'm getting a divide by 0 error
Moods
Moods2y ago
Yeah that would be what I said earlier
MODiX
MODiX2y ago
calledude
REPL Result: Failure
5 % 0
5 % 0
Exception: CompilationErrorException
- Division by constant zero
- Division by constant zero
Compile: 281.084ms | Execution: 0.000ms | React with ❌ to remove this embed.
Moods
Moods2y ago
X doesn’t start from 0 No I mean It shouldn’t If you’re testing for prime stuff You start with 2 You divide, starting from 2 to half the number
AceChewy
AceChewyOP2y ago
so should the for(int x =0; x<= number/2; x++) be for(int x = 2; .....) instead
Moods
Moods2y ago
I just remembered you’re adding these things to a list, it starts from 1 then not 2 :) So for any prime, let’s say 13 It’d go from 1 to 6, and check the mod condition And it should only work for 1 Then you can check after the loop if the count of the list is just 1 You don’t need a list at all btw ! But you do you
AceChewy
AceChewyOP2y ago
I don't need a list, that's right - I just thought it would be easier but I guess not
AceChewy
AceChewyOP2y ago
Right it was this simple :
AceChewy
AceChewyOP2y ago
Thanks @moooodie , @calledude , @ReactiveVeina
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server