Skip to content

Instantly share code, notes, and snippets.

@mark
Created October 10, 2013 03:46
Show Gist options
  • Save mark/6912741 to your computer and use it in GitHub Desktop.
Save mark/6912741 to your computer and use it in GitHub Desktop.
Numbers without zero

Non-zero numbers

This is just something I was toying with—what would our number system be like without a zero digit (which, presumably, was the case before 0 was invented).

I don't think it means anything, but the standard algorithms for addition and multiplication still work.

# Non-zero numbers. Say, in binary. So two symbols, 1 & 2.
bnz = %w[ 1 2 11 12 21 22 111 112 121 122 211 212 221 222 1111 1112 ]
ints = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
bz = %w[ 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 ]
# Note that in binary, 1 = 01 = 001 = 0001, etc. But in non-zero numbers, 1 != 21 != 221, etc.
# Powers of 2: 2 -> 12 -> 112 -> 1112
# Multiples of 2: 2 -> 12 -> 22 -> 112 -> 122 -> 212 -> 222 -> 1112
# Multiples of 3: 11 -> 22 -> 121 -> 212 -> 1111
# Multiples of 4:
# x << 1 = double x + 1
# x << 2 = double x + 2
class Fixnum
def to_snz(base = 2)
return "0" if self == 0
bits = 0
offset = 0
until self >= offset && self < offset + 2 ** bits
offset += 2 ** bits
bits += 1
end
delta = self - offset
deltas = delta.to_s(2)
deltas = deltas.rjust(bits, '0')
deltas = deltas.tr '01', '12'
end
end
100.times do |i|
# puts "#{i}\t#{i.to_snz(2)}\t#{i.to_s(3)}"
end
# Verify:
# ints.each_with_index do |int, idx|
# binary = int.to_s(2)
# binary_non_zero = int.to_snz(2)
#
# puts "i = #{ int }\t#{ binary_non_zero == bnz[idx] ? "OK" : "FAIL" }"
# end
20.times do |i|
m2 = (2*i).to_snz
m3 = (3*i).to_snz
m4 = (4*i).to_snz
m5 = (5*i).to_snz
puts "#{ i }\t#{i.to_snz}\t#{m2}\t#{m3}\t#{m4}\t#{m5}"
end
puts "#{ 123.to_snz } + #{ 345.to_snz } = #{ (123+345).to_snz }"
# require 'prime'
# 10_000.times do |i|
# # if i.prime?
# puts "#{i}\t#{i.to_snz}\t#{i.to_s(2)}"
# # end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment