C
C#13mo 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
Moods13mo 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
JakenVeina13mo ago
why not?
Moods
Moods13mo ago
I could enter abc
JakenVeina
JakenVeina13mo ago
oh, right, it's the string version yeah, you want Int32.TryParse()
AceChewy
AceChewy13mo 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
Moods13mo 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
AceChewy13mo ago
More so to check if a number is prime
Moods
Moods13mo ago
If I entered 2, and I’m reading this correctly, it would check if 2 modulo 0 was equal to 0?
AceChewy
AceChewy13mo 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
Moods13mo ago
Which it does by dividing no? I may be wrong though
MODiX
MODiX13mo 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
Moods13mo 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
AceChewy13mo ago
Yh, I just added that piece of code since you can't get a prime number below 2
Moods
Moods13mo 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
AceChewy13mo ago
Oh yh, that would work - let me try that real quick
AceChewy
AceChewy13mo ago
Right, so I've rearranged some parts now (still not complete
AceChewy
AceChewy13mo ago
How do I ensure that the else branch does nothing and just moves onto the next count of x
Moods
Moods13mo ago
by not having it Also what is the list doing there
AceChewy
AceChewy13mo 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
Moods13mo ago
Ohhhhh
AceChewy
AceChewy13mo 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
Moods13mo ago
Now I’m just curious as to why there’s a loop inside a loop You can just have one loop
AceChewy
AceChewy13mo ago
Wait, the first loop is redundant
AceChewy
AceChewy13mo ago
Ok, I've streamlined some things but now I'm getting a divide by 0 error
Moods
Moods13mo ago
Yeah that would be what I said earlier
MODiX
MODiX13mo 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
Moods13mo 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
AceChewy13mo ago
so should the for(int x =0; x<= number/2; x++) be for(int x = 2; .....) instead
Moods
Moods13mo 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
AceChewy13mo ago
I don't need a list, that's right - I just thought it would be easier but I guess not
AceChewy
AceChewy13mo ago
Right it was this simple :
AceChewy
AceChewy13mo ago
Thanks @moooodie , @calledude , @ReactiveVeina
Accord
Accord13mo 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.