Last active
December 19, 2015 18:09
-
-
Save asterite/5996384 to your computer and use it in GitHub Desktop.
Simple resolution in Crystal for the Google Code Jam discussed here: http://blog.clifreeder.com/blog/2013/04/21/ruby-is-too-slow-for-programming-competitions/
This file contains hidden or 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
| class Int64 | |
| def palindrome? | |
| num = self | |
| reverse = 0_i64 | |
| while num > 0 | |
| dig = num % 10 | |
| reverse = reverse * 10 + dig | |
| num = num / 10 | |
| end | |
| self == reverse | |
| end | |
| end | |
| i = 0 | |
| File.open(ARGV[0], "r") do |file| | |
| file.gets | |
| while line = file.gets | |
| start, finish = line.split(" ").map { |part| part.to_i64 } | |
| sqrt_start = Math.sqrt(start).to_i64 | |
| sqrt_finish = Math.sqrt(finish).to_i64 | |
| j = sqrt_start | |
| found = 0 | |
| while j <= sqrt_finish | |
| if j.palindrome? | |
| square = j * j | |
| if square >= start && square <= finish && square.palindrome? | |
| found += 1 | |
| end | |
| end | |
| j += 1 | |
| end | |
| puts "Case ##{i + 1}: #{found}" | |
| i += 1 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment