aboutsummaryrefslogtreecommitdiffhomepage
path: root/problems/13-Mod-3/a.rb
diff options
context:
space:
mode:
Diffstat (limited to 'problems/13-Mod-3/a.rb')
-rw-r--r--problems/13-Mod-3/a.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/problems/13-Mod-3/a.rb b/problems/13-Mod-3/a.rb
new file mode 100644
index 0000000..b0e909b
--- /dev/null
+++ b/problems/13-Mod-3/a.rb
@@ -0,0 +1,56 @@
+def f1(n)
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ ((n >> 4) & 0b11) +
+ ((n >> 6) & 0b11) +
+ 0
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ ((n >> 4) & 0b11) +
+ ((n >> 6) & 0b11) +
+ 0
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ ((n >> 4) & 0b11) +
+ ((n >> 6) & 0b11) +
+ 0
+ n == 3 ? 0 : n
+end
+
+def f2(n)
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ ((n >> 4) & 0b11) +
+ ((n >> 6) & 0b11) +
+ 0
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ 0
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ 0
+ n == 3 ? 0 : n
+end
+
+def f3(n)
+ n = ((n >> 0) & 0b1111) +
+ ((n >> 4) & 0b1111) +
+ 0
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ ((n >> 4) & 0b11) +
+ 0
+ n = ((n >> 0) & 0b11) +
+ ((n >> 2) & 0b11) +
+ 0
+ n == 3 ? 0 : n
+end
+
+256.times do |n|
+ expected = n % 3
+ actual = f3(n)
+
+ if actual != expected
+ puts "#{actual} != #{expected} (#{n}; #{"%08b" % n})"
+ end
+end