Here's a filter that is O(F=number of factors * P=product of factors) + O(N) to check N numbers. It takes advantage of the fact that the output is periodic: if N is a member, then N + P is also a member. This could be written caching-style, so you only have to do your gcd check if you don't yet have a result for N % P; that would improve your order of efficiency without the potential big up-front cost.
use strict;
use warnings;
{
my @bases = (2,3,5);
my $product = 1;
$product *= $_ for @bases;
my @key;
for my $test (0..$product-1) {
$key[$test] = grep {$test % $_ == 0} @bases;
}
print "Key is <@key>\n";
my $iteration = 1;
sub limbic_sequence {
$iteration = shift if (@_);
while ($iteration++) {
return $iteration if ($key[$iteration % @key]);
}
}
}
print join ', ', map limbic_sequence, 1..50;
print "\n";
printf "First after 100000 is: %d\n", limbic_sequence(100000);
print join ', ', map limbic_sequence, 1..50;
print "\n";
Caution: Contents may have been coded under pressure.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.