I’ve been to solve this task, but this code output is wrong results.
Task:
These primes should fulfill the following requirements:
- The primes should have at least 3 digits.
- The primes can not include the digit 0.
- The sum of the digits should be a multiple of a perfect square number. (Note: even though 1 is a perfect square, do not consider it for obvious reasons)
- The product of the first digit by the last one cannot be 45, or in other words, if 5 is the first digit, the last one cannot be 9.
- The primes’ digits should occur only once. Examples of this kind of primes are:
167, 359
. Cases like113
and331
have to be discarded because digits 1 and 3 that appear twice respectively.
Once he has these special primes, that fulfill the constraints listed above, he has to classify them in three subsets: bouncy, increasing and decreasing primes. The increasing primes are the ones that have their digits in increasing order, for example : 157, 359, 569
.
The decreasing ones, on the other hand, have their digits in decreasing order, for example: 761, 953, 971
.
Finally, the bouncy primes are the ones that does not belong to any of both previous subsets: neither increasing nor decreasing ones, for example: 173, 193, 317
.
Your Task
Do you want to have the results of this investigation and accept the challenge? If your answer is affirmative, continue reading.
Your function will accept an integer higher than 100 (and lower than 50000) as an upper bound of the range to work in, so all these special primes should be lower or equal to the given value of n
.
The function should output a list of lists with the data in this order:
[ [data for _bouncy_ primes], [data for _increasing_ primes], [data for _decreasing_ primes] ]
The data required for each subset should be as follows:
[ min. prime found, max. prime found, number of primes in range ]
Examples
Let’s see some examples for some values of n:
special_primes(101) --> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
No special primes at this value (obviously).
special_primes(200) --> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Still no special primes.
special_primes(457) --> [[251, 439, 2], [349, 457, 4], [431, 431, 1]]
Now we have some values:
- bouncy primes:
251, 439
(2 in total) - increasing primes:
349, 367, 389, 457
(4) - decreasing primes:
431
(1)
special_primes(1000) --> [[251, 947, 11], [349, 479, 5], [431, 983, 4]]
- bouncy primes:
251, 439, 547, 587, 619, 659, 673, 691, 839, 857, 947
(11) - increasing primes:
349, 367, 389, 457, 479
(5) - decreasing primes:
431, 521, 853, 983
(4)
Code:
import math
def is_prime(n):
if n < 2:
return False
for i in range(2, int(math.sqrt(n))+1):
if n % i == 0:
return False
return True
def is_bouncy(n):
digits = list(str(n))
increasing = digits == sorted(digits)
decreasing = digits == sorted(digits, reverse=True)
return not (increasing or decreasing)
def special_primes(n):
bouncy_primes = []
increasing_primes = []
decreasing_primes = []
for num in range(200, n+1):
if is_prime(num):
digits_sum = sum(int(digit) for digit in str(num))
if digits_sum % 4 == 0:
first_digit = int(str(num)[0])
last_digit = int(str(num)[-1])
if first_digit * last_digit == 45:
continue
if len(set(str(num))) != len(str(num)):
continue
if is_bouncy(num):
bouncy_primes.append(num)
elif sorted(str(num)) == list(str(num)):
increasing_primes.append(num)
else:
decreasing_primes.append(num)
bouncy_count = len(bouncy_primes)
increasing_count = len(increasing_primes)
decreasing_count = len(decreasing_primes)
min_bouncy = min(bouncy_primes) if bouncy_count > 0 else 0
max_bouncy = max(bouncy_primes) if bouncy_count > 0 else 0
min_increasing = min(increasing_primes) if increasing_count > 0 else 0
max_increasing = max(increasing_primes) if increasing_count > 0 else 0
min_decreasing = min(decreasing_primes) if decreasing_count > 0 else 0
max_decreasing = max(decreasing_primes) if decreasing_count > 0 else 0
return [
[min_bouncy, max_bouncy, bouncy_count],
[min_increasing, max_increasing, increasing_count],
[min_decreasing, max_decreasing, decreasing_count]
]
print(special_primes(101)) #output:[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(special_primes(200)) #output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(special_primes(457)) #output: [[251, 439, 2], [349, 457, 4], [431, 431, 1]]
print(special_primes(500)) #output: [[251, 439, 2], [349, 479, 5], [431, 431, 1]]
print(special_primes(1000)) #output: [[251, 947, 11], [349, 479, 5], [431, 983, 4]]
print(special_primes(5000)) #output: [[251, 4987, 58], [349, 4789, 13], [431, 983, 4]]