Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Spooky math - with Perl

by tilly (Archbishop)
on Nov 02, 2000 at 18:08 UTC ( [id://39630]=perlmeditation: 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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://39630]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2025-06-18 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.