Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This is a revisit of Spooky math problem.

Fastolfe said that the math confused him, and asked to see some Perl. turnstep posted some Perl, but it missed the key point. So here is my try at some explanatory Perl.

This is still a little raw. But I don't have time to do more with it, and you can see the thinking process. Note in particular the "is_guaran" field - that tracks the heart of why this spooky math works.

Give it a shot. Read this through to see how it works, run it, and follow through the explanation I gave earlier. This weekend I will try to catch up on any remaining confusion.

#! /usr/bin/perl -w use strict; use Getopt::Std; use vars qw(@nums $opt_n $opt_N); getopts("n:N:"); use Data::Dumper; $Data::Dumper::Indent = 1; if ($opt_n) { @nums = split /:/, $opt_n; unless (2 == @nums) { die "Need -n option to have an argument [number:number]\n"; } } else { @nums = (ask_num(), ask_num()); } unless ($opt_N) { print "How many times should I run this: "; $opt_N = <STDIN>; chomp($opt_N); } for (1..$opt_N) { my %info = run_experiment(@nums); print Dumper(\%info); <STDIN>; } sub ask_num { print "Please enter a number: "; my $num = <STDIN>; chomp($num); return $num; } sub flip_coin { 0.5 > rand() ? 1 : 0; } sub guess_other_num { return (log(rand) * (flip_coin() ? 10 : -10 )); } sub run_experiment { my %status; @status{'low', 'high'} = sort {$a <=> $b} @_; if (flip_coin()) { $status{"gave_high"} = 1; @status{"guess_high", "aux"} = think_high($status{high}); } else { $status{"gave_high"} = 0; @status{"guess_high", "aux"} = think_high($status{low}); } $status{is_right} = ($status{guess_high} == $status{gave_high}); if ($status{low} < $status{aux} and $status{aux} < $status{high}) { $status{is_guaran} = 1; } else { $status{is_guaran} = 0; } if (wantarray) { return %status; } else { return $status{is_right}; } } sub think_high { my $num = shift; my $other_num = guess_other_num(); my $guess = $num > $other_num; return wantarray() ? ($guess, $other_num) : $guess; }

In reply to Spooky math - with Perl by tilly

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.
  • Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2024-10-09 13:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The PerlMonks site front end has:





      Results (45 votes). Check out past polls.

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.