Perrin primes
The Perrin sequence is defined to start with 3, 0, 2 and after that, term n is the sum of terms n - 2 and n - 3. So far as I can see this sequence has no obvious practical use, but that's what pure mathematicians like. Perrin primes are then those terms in the sequence which are prime numbers.
Looking at the supplied value of f(13) it seems wise to invoke our old friend Math::BigInt, and the only other quirk is that some early numbers - such as 5 - appear more than once so duplicates need to be avoided.
f(23) is (allegedly):
3631640163992448158050321979101634523424467070532989940589376200895999542521324121865744873084026078365592113103829057044319371093458267314150632770247926037880226504980936257910602481948018841362454143562440537190514898173776176693598426395086189616722660098879586330664613823090197360409779437591689520837492830513163054777061491401259817572546197753109857199993236881971656255401039799820579630315398215866349742611432329007353997352494443986017317922833363523351835711663212252398827126207580953779469798265218514506497114067477064259789799733135324524166520280952689291443318735365943242441087374207019201381566622887361047383284786893087439845660097204995566088460835424395601898782600822606786314286293
... and beyond that is_prime begins to struggle.
Home primes
The home prime HP(n) of an integer n is obtained by concatenating its prime factors (in ascending order) to form a new number and then repeating this process until the result is prime. Perl is good at switching between the binary and character representation of a number as the context demands, so if we are allowed factor and is_prime from Math::Util this task is fairly trivial:
sub home {
my @prime_factors = factor(shift);
my $concat .= $_ for @prime_factors;
return $concat if is_prime($concat);
home($concat);
}
This works in microseconds up to the 48th home prime, but the 49th is apparently yet to be solved.
No comments:
Post a Comment