On Nov 18, 2007 5:37 PM, James Koppel <jamesbkoppel / yahoo.com> wrote:
> Obviously, I didn't know about the mathn library when I wrote this:
>
> PRIMES = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
>                 73,79,83,89,97,101,103,107,109,113,

I also hadn't found the mathn library yet when I wrote my first
version. Below are two versions: my first working version and the
current version. I like the current version much better, but the
lengths are almost the same. I really like being able to add methods
to existing classes like this.

# begin first version

class Encryption
	def Encryption.encode(msg,prev_prime=1)
		return 1 if msg.size.zero?
		this_prime = next_prime(prev_prime)
		return (this_prime ** (1 + msg[0])) * encode(msg.slice(1,msg.size),this_prime)
	end
	def Encryption.decode(num,prev_prime=1)
		return "" unless num > 1
		this_prime = next_prime(prev_prime)
		multiplicity = factor_multiplicity(this_prime,num)
		(multiplicity-1).chr + Encryption.decode(num / (this_prime **
multiplicity), this_prime)
	end
	private
	def Encryption.prime?(num)
		(num - 1).downto(2) {|factor| return false if num.modulo(factor).zero?}
		true
	end
	def Encryption.next_prime(prev)
		n = prev + 1
		return n if prime?(n)
		next_prime(n)
	end
	def Encryption.factor_multiplicity(factor,num)
		1.upto(num) {|x| return x - 1 unless num.modulo(factor**x).zero?}
	end
end

puts "Test encoding: "+Encryption.encode("Ruby\n").to_s+"\n"
puts "Test decoding: "+Encryption.decode(Encryption.encode("Ruby\n"))+"\n"

# end first version

# begin current version

require 'mathn'

class Prime
	def last
		@primes.last
	end
end

class String
	def to_godel(primes=Prime.new)
		return 1 if size.zero?
		return (primes.next ** (1 + self[0])) * slice(1,size).to_godel(primes)
	end
	def self.from_godel(num,primes=Prime.new)
		return "" unless num > 1
		multiplicity = factor_multiplicity(primes.next,num)
		(multiplicity-1).chr + from_godel(num / (primes.last ** multiplicity), primes)
	end
	private
	def self.factor_multiplicity(factor,num)
		1.upto(num) {|x| return x - 1 unless num.modulo(factor**x).zero?}
	end
end

puts "Test encoding: "+"Ruby\n".to_godel.to_s+"\n"
puts "Test decoding: "+String.from_godel("Ruby\n".to_godel)+"\n"

# end current version

-- 
There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult.

                    - C.A.R. Hoare -