such as any cache mechanism. so I can re run it fast for debug?

Yes, there are several ways to do this. Which data structure would you like to cache? Since in my tests, using the assumption I showed below, I was able to regenerate @arr_abundant very quickly, I'm guessing it would make sense to cache @arr_sum_two_abundant_numbers? Since both are simple arrays of integers, you could write either of them to a file, each integer on a line. Or, you could use one of the many serialization formats like JSON, YAML, XML, etc. There's also the core module Storable, which reads and writes Perl data structures in a binary format, but it does have one possibly very important caveat, which is that such files are often not compatible between different versions of the module. If you're just using it for temporary caching on one machine, it's fine, but it's not the right solution for long-term storage or sharing between machines. Here, I've shown both the "plain file" method (using Path::Class to simplify the reading/writing, although it could easily be done in plain Perl), as well as Storable, commented out.

use warnings; use strict; use Math::Prime::Util::GMP qw/sigma/; use Path::Class qw/file/; #OR: use Storable qw/store retrieve/; my $cache_file = '/tmp/abundant_sums_cache'; my @arr_sum_two_abundant_numbers; if (-e $cache_file) { @arr_sum_two_abundant_numbers = file($cache_file)->slurp(chomp=>1) +; #OR: @arr_sum_two_abundant_numbers = @{ retrieve($cache_file) }; } if (!@arr_sum_two_abundant_numbers) { print "regenerating \@arr_sum_two_abundant_numbers...\n"; my @arr_abundant = grep { sigma($_)>2*$_ } 12 .. 28123; my %hash; foreach my $i (@arr_abundant) { foreach my $j (@arr_abundant) { my $positive = $i + $j; next if $positive > 28123; $hash{$positive}++; } } @arr_sum_two_abundant_numbers = sort {$a <=> $b} keys %hash; file($cache_file)->spew_lines(\@arr_sum_two_abundant_numbers); #OR: store(\@arr_sum_two_abundant_numbers, $cache_file); } else { print "loaded \@arr_sum_two_abundant_numbers from $cache_file\n +" } my $sum_total; my $n = 1; foreach my $key (@arr_sum_two_abundant_numbers) { for ( my $i=$n ; $i < $key ; $i++) { $sum_total += $i; } $n = $key +1; } print "$sum_total\n";

Because your question seems to be more about the caching, and because you don't show your data structures, explain what prob23_type_of_number is (no Google results other than this node*), or show the expected output, I haven't yet spent a lot of time on understanding your algorithm or whether it can be optimized. However, I did notice one thing:

an array named "@arr_abundant" which contain nearly 30000 elements.

I am guessing/assuming these are Abundant numbers (OEIS A005101), then as far as I can tell there are 6965 such numbers from 12 to 28123:

use Math::Prime::Util::GMP qw/sigma/; print grep( { sigma($_)>2*$_ } 12 .. 28123 )."\n"; __END__ 6965

Update: Updated wording, a few minor additions to explanation.

* Update 2: Is this Project Euler Problem 23? Final Update: Since the output of my code matches that of other solutions I found, I guess it is :-)


In reply to Re: Is there any cache mechanism for running perl script by haukex
in thread Is there any cache mechanism for running perl script by DillonYu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.