class E51 def numDigits(x as int) as int if x < 10 return 1 else return 1 + .numDigits(x // 10) def isPrime(p as int) as bool if p < 2 or p % 2 == 0 return p == 2 n = 3 while n*n <= p if p % n == 0 return false n += 2 return true def numPrimesSameChange2(p as int) as int digs = .numDigits(p) + 2 count as int = 0 ans = 0 for i in 0:digs-1 for j in i+1:digs tcount as int = 0 b = (p % (10 ** i)) + (p // (10 ** i)) * (10 ** (i+1)) b = (b % (10 ** j)) + (b // (10 ** j)) * (10 ** (j+1)) seenOne = false tempans = 0 for k in 0:10 if .numDigits(b) < .numDigits(10 ** j) and k == 0 #cant have leading 0s continue if .isPrime(b + k*(10 ** i + 10 ** j)) tcount += 1 if not seenOne tempans = (b + k*(10 ** i + 10 ** j)) seenOne = true if tcount > count count = tcount ans = tempans if count == 8 print ans return count def numPrimesSameChange3(p as int) as int digs = .numDigits(p) + 3 count as int = 0 ans = 0 for i in 0:digs-2 for j in i+1:digs-1 for k in j+1:digs tcount as int = 0 b = (p % (10 ** i)) + (p // (10 ** i)) * (10 ** (i+1)) b = (b % (10 ** j)) + (b // (10 ** j)) * (10 ** (j+1)) b = (b % (10 ** k)) + (b // (10 ** k)) * (10 ** (k+1)) seenOne = false tempans = 0 for l in 0:10 if .numDigits(b) < .numDigits(10 ** k) and l == 0 #cant have leading 0s continue if .isPrime(b + l*(10 ** i + 10 ** j + 10 ** k)) tcount += 1 if not seenOne tempans = (b + l*(10 ** i + 10 ** j + 10 ** k)) seenOne = true if tcount > count count = tcount ans = tempans if count == 8 print ans return count def main for p in 10:10000 if .numPrimesSameChange3(p) >= 8 or .numPrimesSameChange2(p) >= 8 break
Friday, October 11, 2013
Problem 51 - Cobra
Cobra is, surprisingly, a language based on Python, but with lots of other exciting stuff like optional static typing, generics, and more object-orientedness (all code must be wrapped in a class with a main method...as such all functions are methods of a class, and need to be called with a preceding . as shorthand for this. - forgetting that . was the source of all my errors in this problem). Anyway, despite a few quirks that took getting used to, it was almost as nice to work with as Python, so it didn't take too hard to solve this relatively hard problem (though it took a change to a smart approach to solve: iterating over all primes is very slow, so instead I iterate over 'masks' to make primes out of [the problem is to find what prime is the smallest prime that generates 8 primes if you replace some digits of the number with the same digit for 0..9]. Now I have moved past 50, and between this problem and my memory, the problems do start to get moderately hard from here on out. I imagine I won't be writing too many new solutions in new languages for too much longer, as the supply of nice, easy-to-learn languages is constantly dwindling. I do still have Squirrel around as an option though, and I think that I will allow C++ and C# to be options in the future, instead of lumping them into the same 'language' as C.
runs in .1s
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment