In fairness, this is based on a StackExchange question - but its a good question.
The challenge is fairly simple:
- Take a string of numerals
- Find and print the largest contiguous prime number in the string
Scoring:
- Lowest number of characters wins.
- Victor will likely be a golfscript entry but we won't hold that against them, cause we all have fun and learn things, right.
- Winner we be awarded when I notice that I haven't actually ticked the green button.
Assumptions:
- The string is only numbers
- If the string contains letters, you may have undefined behaviour
- The string contains at least 1 prime
- If the string does not contains 1 valid prime number, you may have undefined behaviour
- Speed is not a constraint
- Use a shorter prime algorithm over a faster one.
- If your entry eventually finishes, thats ok, just make sure it will provable happen before the heat death of the universe.
- The length of the string can be assumed less than 15 characters long
For example:
>> Input: 3571
<< Output: 3571
>> Input: 123
<< Output: 23
>> Input: 1236503
<< Output: 236503
>> Input: 46462
<< Output: 2
>> Input: 4684
<< Output: ValueError: max() arg is an empty sequence
>> Input: 460
<< Output: 0 # Note, zero is not a prime, but the above string has no valid prime
>> Input: 4601
<< Output: 601
>> Input: "12 monkeys is a pretty good movie, but not as good as se7en"
<< Output: ValueError: Fight Club was also good, I find Brad Pitt to be a consistantly good actor.
Possible implementations:
- Find all substrings of the input, check if they are prime. - Legostormtroopr (original)
- Find all integers less than input, check if they are in the input then check if it is prime - Ben Reich
- Take a list of all primes less than the input, check if it is in the input - daniero