#!/usr/bin/perl

use 5.040;

### Como este script me ha quedado bastante bien,
### (c) Jesús Lozano Mosterín, 2025

my $min = 9e99;

say "\nEl objetivo es sqrt(2) dado 2 y, como comprobación, sqrt(2)*sqrt(2) == 2\n";

my $INPUT = 2;        # part of the secret function 

my $try = $min;
my $result = 0;

while ($min >= 1){
    $try = $result + signo($INPUT) * int( $min * rand() );
    
    if ( abs(objective($INPUT) - $try) < $min ){
	$min = abs(objective($INPUT) - $try);
	$result = $try;
	say "Parte entera:  $result -> error $min";
    }
}
say "-" x 80;
while ($min > 0){   
    $try = $result + signo($INPUT) * rand() * $min;  #  * $min;                    # "1.0e-$num"; 
    
    my $tmp = abs(objective($INPUT) - $try);
    if ( $tmp < $min && $min >= 0){
	$min = $tmp;
	$result = $try;
	say "Parte decimal:  $result -> error $min";
	exit 10 if $min == 0 && comprobe($result) == 1;
    }
}

exit 2;


sub objective {
    my $i = shift;
    $i //= $INPUT;       # test standard MonteCarlo
    return sqrt($i);     # This is the target example. Could be any function 
}
    
sub signo {
    my $j = shift;
    if (objective($j) > $try){
	return 1;
    }else{
	return -1;
    }
}

sub comprobe {
    my $k = shift;      # for the test, despite is a redundant one. $min == 0 will suffer
    return 1 if ($k * $k == $INPUT);
    return 0;
}

__END__
  
Al parecer, ya no es necesario $resultant para 'aprender'
  de la mejor solución anterior.
  

