This is also sadly brute-forcish:
Assuming this program works (and I have no idea if it does) you can figure out an arbitrary number of pirates greater than 1. The answer to 8 pirates is (SPOILER)390621. The answer to 9 pirates is (SPOILER)1953121. The answer to 10 pirates is (SPOILER)9765621. This program appears to take exponentially more time as you increase the number of pirates linearly, which means it's a sucky algorithm. 11 pirates would take many minutes to solve.Code:#!/usr/bin/ruby def find_num(x, try, max) # Output intermediary steps if you want # puts "#{x},#{try},#{max}" return false if x % 5 != 1 return true if try == max return x if find_num( (x-1) * 4/5, try+1, max) end 1.upto(99999999999) do |x| if(ans = find_num(x, 1, 5)) puts "Answer: #{ans}" break end end



Reply With Quote