20
4
Your goal is to determine if a number is divisible by 3 without using conditionals. The input will be an unsigned 8 bit number from 0 to 255. Creativity encouraged!
You are ONLY allowed to use
Equality/Inequality (
==
,!=
,>
,<
,>=
,<=
)Arithmetic (
+
,-
,x
)Logical Operators (
!
not,&&
and,||
or)Bitwise Operators (
~
not,&
and,|
or,^
xor,<<
,>>
,>>>
arithmetic and logical left and right shifts)Constants (it would be better if you kept these small)
Variable assignment
Output 0
if false, 1
if true.
Standard atomic code-golf rules apply. If you have any questions please leave them in the comments. Example methods here. A token is any of the above excluding constants and variables.
@GregHewgill My typo, it should be 8 bit number. – qwr – 2014-06-23T03:33:58.827
2Are we only allowed to use the above operators? Otherwise, modulo would make this way too easy. – Jwosty – 2014-06-23T03:40:41.697
Also, how about table lookup? – Greg Hewgill – 2014-06-23T03:42:13.067
@Jwosty you are only allowed to use those, but I will take suggestions. – qwr – 2014-06-23T03:44:45.753
@GregHewgill If you use a table lookup, you have to count all tokens. – qwr – 2014-06-23T03:46:19.403
Is 0 considered to be divisible by 3? – Greg Hewgill – 2014-06-23T03:49:45.637
3Can you clarify what you mean by no conditionals? Is it limited to IF statements, or does it apply to things like loops as well? – Ruslan – 2014-06-23T03:50:09.927
1@Ruslan You are only allowed to use the above. – qwr – 2014-06-23T03:54:02.297
Can I return
true
/false
instead of1
/0
? – Tyilo – 2014-06-23T17:20:12.533what about Ruby's to_s or to_i? That is, toString or toInteger – Not that Charles – 2014-06-23T21:52:34.043