Created
February 21, 2013 09:22
-
-
Save fearofcode/5003443 to your computer and use it in GitHub Desktop.
See http://www.zerocater.com/challenge/#mathy-challenge for details of what this does
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See http://www.zerocater.com/challenge/#mathy-challenge for details of what | |
# this code does | |
# | |
# The approach I took was to Google 'primal sums' and get most of the solution | |
# directly: | |
# | |
# http://oeis.org/A007506 | |
# | |
# I also use pre-computed lists of primes to avoid having to implement a sieve | |
# in order to get lists of primes. | |
# | |
# This code uses the above knowledge of the last digit in the 'fifth chain' sequence | |
# to get the fifth chain without having to directly compute it. See below. | |
# | |
# Requires a system with the 'unzip' command in the path. This code was executed on | |
# a standard machine running Ubuntu 12.10 in ~10 seconds (including I/O). | |
require 'open-uri' | |
def read_digit_list(url, lines_to_drop) | |
puts "Downloading file at URL #{url}" | |
primes_list_txt = open(url) { |f| f.read } | |
puts "Done." | |
# get rid of the header/introductory lines | |
primes_list_pruned = primes_list_txt.split("\n").drop(lines_to_drop).join("\n") | |
primes_list_pruned.scan(/\d+/).collect(&:to_i) | |
end | |
def chain_search(digits, min_last) | |
digit_list = [] | |
sum = 0 | |
digits.each do |digit| | |
digit_list << digit | |
sum += digit | |
last_digit = digit_list.last | |
if sum % last_digit == 0 && last_digit > min_last | |
return digit_list.length | |
end | |
end | |
end | |
small_digits = read_digit_list("http://primes.utm.edu/lists/small/100000.txt", 3) | |
puts chain_search(small_digits, 5) # third chain (already known) | |
puts chain_search(small_digits, 71) # fourth chain (last digit 369119) | |
# To find the fifth chain length, we only have to find the position of the digit | |
# 415074643 (which we got in the Sloane integer sequence above) in a list of | |
# primes found on the same server as the 100,000 list above. It's in the 23 | |
# million range. It's stored as a zip file on the UTM server, so we'll download | |
# the zip, extract it locally, then read it as a file. | |
target_digit = 415_074_643 | |
offset = 22_000_000 | |
puts "Downloading 22-23 million primes list" | |
big_prime_url = "http://primes.utm.edu/lists/small/millions/primes23.zip" | |
puts "Done. Writing and extracting the file" | |
zip_download = open(big_prime_url) { |f| f.read } | |
File.open('primes23.zip', 'w') {|f| f.write(zip_download) } | |
`unzip primes23.zip` | |
puts "Done." | |
big_digit_list = read_digit_list('primes23.txt', 2) | |
index = big_digit_list.index(target_digit) | |
final_length = offset + index | |
puts final_length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment