Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
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 chanting in the Monastery: (5)
As of 2024-03-29 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found