+ 13

[challenge] check if a number is even without modulo operator or floor devision.

Modulo operator: 8/5 = 1 remainder 3 8%5 = 3 floor devision : 44//5 = 8.

17th Jan 2018, 3:40 PM
Oma Falk
Oma Falk - avatar
33 Antworten
17th Jan 2018, 6:57 PM
LukArToDo
LukArToDo - avatar
+ 10
Ruby: > 4.even? => true
18th Jan 2018, 2:33 AM
noobcøder
noobcøder - avatar
17th Jan 2018, 4:18 PM
Cool Codin
Cool Codin - avatar
17th Jan 2018, 6:03 PM
Al Toe
+ 5
https://code.sololearn.com/cC8FCmroHcN5/?ref=app
18th Jan 2018, 7:48 AM
🐙evil octopus
🐙evil octopus - avatar
18th Jan 2018, 11:58 AM
Nashat
Nashat - avatar
24th Jan 2018, 11:37 PM
Med Arezki
Med Arezki - avatar
+ 4
@code learner up to now the solutions are based on binary operators. I just ask myself if this "trick" works for the number 8.
17th Jan 2018, 6:00 PM
Oma Falk
Oma Falk - avatar
+ 4
@alfred your code works for every number! you perfectly hide a floor devision by using integer division. This "trick" is not possible in every language It is allowed and ... thumbs up for you!
17th Jan 2018, 6:18 PM
Oma Falk
Oma Falk - avatar
+ 4
@vcc you solution counts as 1/2 - liner (-;
18th Jan 2018, 5:44 AM
Oma Falk
Oma Falk - avatar
+ 3
@cool codin and @paul perfect solution. Can you do it with again to check if a number is devisible by 8? #Edit easy for all powers of 2- it is similiar I have no solution for other numbers yet.
17th Jan 2018, 5:49 PM
Oma Falk
Oma Falk - avatar
+ 3
print(["even","odd"][x&1])
18th Jan 2018, 5:36 AM
VcC
VcC - avatar
18th Jan 2018, 6:55 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
@infinity perfect! now check devisible by 8 if you wanna
18th Jan 2018, 7:17 AM
Oma Falk
Oma Falk - avatar
+ 3
here is the solution for 8... :D https://code.sololearn.com/cAR135H32Zcq/?ref=app
18th Jan 2018, 8:44 AM
🐙evil octopus
🐙evil octopus - avatar
+ 3
num & 1 ? "odd" : "even"
18th Jan 2018, 11:27 AM
---
--- - avatar
+ 3
I made this code in Python that checks if one number is divisible by another. I tried to train with exceptions but inputs don't work well on the playground, so make sure to actually write an integer. Oh, and the code is not really elegant, I would gladly accept some advice for that https://code.sololearn.com/cMKqsWnL61Hb/?ref=app
18th Jan 2018, 10:54 PM
MakoT
MakoT - avatar
+ 2
I saw the bitwise or operator today in a code from ++EviSolo and made use of it now (I am sorry it is not a oneliner): https://code.sololearn.com/c0QEy4cmik6W/?ref=app
17th Jan 2018, 5:11 PM
Paul
Paul - avatar
+ 2
My code works for 8 now too. Just by checking the 3 most right bits.
17th Jan 2018, 6:38 PM
Paul
Paul - avatar
+ 2
Java -> int x = 10; return x/2*2 == x ? "Even" : "Odd"; Other language -> use truncate
18th Jan 2018, 10:39 AM
Momo Belia
Momo Belia - avatar