http://www.perlmonks.org?node_id=39630

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; }