Skip to content

Instantly share code, notes, and snippets.

@lpar
Created November 3, 2011 14:05
Show Gist options
  • Select an option

  • Save lpar/1336562 to your computer and use it in GitHub Desktop.

Select an option

Save lpar/1336562 to your computer and use it in GitHub Desktop.
Simple Gödel encoding and decoding
#!/usr/bin/env ruby
# encoding: UTF-8
#
# Simple Gödel encoding
# Ruby 1.9.3
require 'prime'
# Allowed characters
CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def decode(encoded)
decoded = ''
primes = Prime::EratosthenesGenerator.new
while !decoded.match(' ')
prime = primes.next
temp = encoded
pow = 0
while (temp % prime) == 0
temp = temp / prime
pow += 1
end
decoded << CHARS[pow]
end
return decoded.strip
end
def encode(plaintext)
encoded = 1
primes = Prime::EratosthenesGenerator.new
i = 0
# Ensure no double-spaces in source text, as that
# indicates message termination when decoding
text = plaintext.gsub(/ */, ' ').upcase
text.each_char do |x|
c = CHARS.index(x)
if c
i += 1
encoded = encoded * primes.next**c
end
end
return encoded
end
puts encode('All work and no play makes Jack a dull boy')
puts decode(302841943913193385738713551625817000427562261942842587929015537746428183661378187315559771978420129418887436160028297029588657156437015075648670687785809963529258353218547333325583147736258864907770101429735906490566082415554531521796426549706253542076864961225050164062500000000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment