I think it's easiest to explain this challenge in a sequential manner. Start with an input number \$N\$ and:
- Find its highest prime factor
- Check numbers above and below \$N\$ and see if the highest prime factor is higher (i.e. the highest prime factor of \$N-1\$ and/or \$N+1\$ is higher than the factor of \$N\$.
- Continue to check higher and/or lower numbers neighboring \$N\$ in the directions where the highest factors are increasing (\$(N-2, N-3 ...)\$ and/or \$(N+2, N+3 ...)\$ and so on)
- Once there aren't any prime factors in either direction that are higher than the ones we've already found we stop and output the highest prime factor we have encountered.
Let's look at an example:
245
has the prime factors 5, 7, 7
. Its neighbors are:
244 -> 2, 2, 61
245 -> 5, 7, 7
246 -> 2, 3, 41
The highest prime factor is increasing in both direction, so we must look at the next neighbor:
243 -> 3, 3, 3, 3, 3
244 -> 2, 2, 2, 61
245 -> 5, 7, 7
246 -> 2, 3, 41
247 -> 13, 19
The highest prime factors are now decreasing in both direction, so the highest prime factor we've encountered is 61
, and should therefore be returned.
Another example:
Let's look at 1024
. Its prime factors are 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
.
The prime factors of its nearest neighbors are:
1023 -> 3, 11, 31
1024 -> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
1025 -> 5, 5, 41
The highest prime factor are increasing in both direction, from 2
to 31
or 41
. Let's look at the neighbors:
1022 -> 2, 7, 73
1023 -> 3, 11, 31
1024 -> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
1025 -> 5, 5, 41
1026 -> 2, 3, 3, 19
The highest prime factor for 1022
is 73
, and the highest prime factor for 1026
is 19
. Since 19
is lower than 41
we're not interested in it. It's still increasing for numbers smaller than \$N\$, so we'll check the next one in that direction:
1021 -> 1021
1022 -> 2, 7, 73
1023 -> 3, 11, 31
1024 -> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
1025 -> 5, 5, 41
1026 -> 2, 3, 3, 19
1021
is a prime, and the highest prime we've encountered, so it should be returned.
Rules:
- You will only get positive \$N\$ larger than \$1\$ and smaller than \$2^{31}-2\$.
- Input and output formats are optional, but the numbers must be in base 10.
- You should continue searching for higher primes as long as the highest value is increasing in that direction. The directions are independent of each other.
Test cases:
Format: N, highest_factor
2, 3
3, 3
6, 7
8, 11
24, 23
1000, 997
736709, 5417
8469038, 9431
2
for N. We then get5
for N-1 and61
for N+1. Then we get19
for N-2 and67
for N+2. Should we keep trying lower numbers, since19>5
or stop, since5<61
? I.e. are the maxima kept per-side? (I'm not sure if the example is mathematically possible.) \$\endgroup\$N=2
actually seems to be an edge case since1
has no prime factors, so no maximum prime factor with which we may compare in order to decide whether we should continue. \$\endgroup\$