Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
One thing to realize is that it makes sense to only apply "floor" after applying "sqrt", and that always applying "floor" after doing "sqrt" doesn't eliminate any solutions. This means there are only two operations: taking the faculty of a number, or to take the square root and flooring the number. This also means all intermediate values are integers.

The following algorithm is a breadth-first search, keeping track of which numbers were already calculated. To keep the intermediate values managable, it won't apply faculty on numbers greater than 200 (this is a tight limit, for at least one of the number 1 .. 10, you need the faculty of 200). After finding solutions for the numbers 1 to 10, it prints them.

#!/usr/bin/perl use strict; use warnings; use Math::BigInt; use constant FAC_MAX => 200; sub pretty_print; my $three = {meth => "init", value => Math::BigInt->new(3), old_value => Math::BigInt->new(3)}; my @list = ($three); my %done = map {$_->{value} => $_} @list; while (@list) { my $node = shift @list; for my $meth (qw /bsqrt bfac/) { next if $meth eq 'bfac' && $node->{value} > FAC_MAX; my $new_value = $node->{value}->copy->$meth; unless ($done{$new_value}) { my $new = {meth => $meth, value => $new_value, old_value => $node->{value}}; push @list, $new; $done{$new_value} = $new; } } my $c = 0; $done{$_} && $c++ for 1 .. 10; if ($c >= 10) {last} } for my $n (1 .. 10) { printf "%2d = ", $n; pretty_print $done{$n}; print "\n"; } sub pretty_print { my $n = shift; my $m = $n->{meth}; if ($m eq "bsqrt") { print "|sqrt("; pretty_print($done{$n->{old_value}}); print ")|"; } elsif ($m eq "bfac") { print "("; pretty_print($done{$n->{old_value}}); print ")!"; } elsif ($m eq "init") { print $n->{value} } else {die} } __END__ 1 = |sqrt(3)| 2 = |sqrt((3)!)| 3 = 3 4 = |sqrt(|sqrt(|sqrt(|sqrt(|sqrt(|sqrt((|sqrt(|sqrt(|sqrt(|sqrt(|sqr +t(|sqrt(|sqrt(((|sqrt(|sqrt(((3)!)!)|)|)!)!)|)|)|)|)|)|)|)!)|)|)|)|)| +)| 5 = |sqrt(|sqrt(((3)!)!)|)| 6 = (3)! 7 = |sqrt(|sqrt(|sqrt(|sqrt(|sqrt(|sqrt((|sqrt(|sqrt(|sqrt(|sqrt((|sq +rt(((3)!)!)|)!)|)|)|)|)!)|)|)|)|)|)| 8 = |sqrt(|sqrt((|sqrt(|sqrt(|sqrt(|sqrt(|sqrt(|sqrt((|sqrt(|sqrt(|sq +rt(|sqrt((|sqrt(((3)!)!)|)!)|)|)|)|)!)|)|)|)|)|)|)!)|)| 9 = |sqrt(|sqrt(|sqrt(|sqrt(|sqrt((|sqrt(|sqrt(|sqrt(|sqrt(|sqrt(|sqr +t(|sqrt(|sqrt((|sqrt((|sqrt(|sqrt((|sqrt(|sqrt(|sqrt(|sqrt(|sqrt(|sqr +t((|sqrt(|sqrt(|sqrt(|sqrt((|sqrt(((3)!)!)|)!)|)|)|)|)!)|)|)|)|)|)|)! +)|)|)!)|)!)|)|)|)|)|)|)|)|)!)|)|)|)|)| 10 = |sqrt((|sqrt(|sqrt(((3)!)!)|)|)!)|

In reply to Re: Challenge: Chasing Knuth's Conjecture by Anonymous Monk
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 having an uproarious good time at the Monastery: (4)
    As of 2019-11-14 06:12 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (76 votes). Check out past polls.