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