Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I took your ideas for avoiding re-testing things and revamped my sieve. This generates all the answers that don't require factorial on anything higher than whatever you set (350 gets me all solutions for 1..30). It runs in about 5 seconds on my 1 GHz PC with that setting.
use strict; use warnings; use bigint; # How high a number are you willing to factorialize? my $factorial_limit = 999; # Pre-cache them to save on calls my @factorials = do { my $last_fact = 1; (0, 1, map { $last_fact *= $_ + } 2..$factorial_limit) }; # What's the biggest number you want to print? my $print_up_to = 200; # Each element of @formulas is an an op-string: # a string of Fs and Ss, indicating factorial # and square root, in order applied to get the # index my %formulas = (3 => '3'); my @unapplied = ([3, '3']); while (@unapplied) { my ($v, $ops) = @{shift(@unapplied)}; # Apply factorial if ($v <= $factorial_limit) { my $fv = $factorials[$v]; my $fops = $ops . 'F'; if (not exists $formulas{$fv}) { push @unapplied, [$fv, $fops]; $formulas{$fv} = $fops; } } # Apply sqrt $v = int(sqrt($v)); $ops .= 'S'; if (not exists $formulas{$v}) { push @unapplied, [$v, $ops]; $formulas{$v} = $ops; } } for (sort { $a <=> $b } keys %formulas) { last if $_ > $print_up_to; print "$_: $formulas{$_}\n"; }
Update: pregenerated factorials to save time; also put in a limit on how high to print, so the huge numbers don't show up. Even taking factorials as high as 999 (takes 30-40 seconds), there's no solution for 38.

Caution: Contents may have been coded under pressure.

In reply to Re^2: Challenge: Chasing Knuth's Conjecture by Roy Johnson
in thread Challenge: Chasing Knuth's Conjecture by kvale

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2019-11-18 12:04 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (90 votes). Check out past polls.