note 85735 modified in ref.math by danbrown

From: Date: Fri, 07 May 2010 12:29:58 +0000
Subject: note 85735 modified in ref.math by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169048@lists.php.net to get a copy of this message
Recently I needed to determine the size of N is unknown, only the state of N is known. May seem like a weird thing to need, but a example is finding the maximum size of sending email to a SMTP server when the maximum size is currently unknown. You can send $size email each iteration and the direction will be TRUE if the email is to big and is rejected by the server, or FALSE if the email is accepted. Below is a example. <?php // our arbitrary number that if not being used in this example, would be // otherwise unknown $n = 1; echo "Finding: $n\n"; $direction = FALSE; $step = 1; $size = $sizeLeast = $sizeMax = $i = 0; while(1) { // small var for counting, could be taken out if you don't care how many // iterations it took (in common use you wouldn't) $i++; // this should be a function call or something that says what the current // state of N is, above (true) or below (false), here $n is just a arbitrary // number and to determine the state we compare it against current $size $direction = ($size > $n); // simple steps based on the current state of n (direction), the call could // be here instead of assigning direction for more performance if($direction) { $sizeMax = $size; $step = (int) round($step/2); $size -= $step; } else { $sizeLeast = $size; $step = $step*2; $size += $step; } // if sizemax - sizeleast is 1, then obviously the size is sizeLeast if(1 === ($sizeMax - $sizeLeast) && 1 === $step) { break; } } echo "Found: $size in $i iterations\n"; ?> --was-- Recently I needed to determine the size of N is unknown, only the state of N is known. May seem like a weird thing to need, but a example is finding the maximum size of sending email to a SMTP server when the maximum size is currently unknown. You can send $size email each iteration and the direction will be TRUE if the email is to big and is rejected by the server, or FALSE if the email is accepted. Below is a example. // our arbitrary number that if not being used in this example, would be // otherwise unknown $n = 1; echo "Finding: $n\n"; $direction = FALSE; $step = 1; $size = $sizeLeast = $sizeMax = $i = 0; while(1) { // small var for counting, could be taken out if you don't care how many // iterations it took (in common use you wouldn't) $i++; // this should be a function call or something that says what the current // state of N is, above (true) or below (false), here $n is just a arbitrary // number and to determine the state we compare it against current $size $direction = ($size > $n); // simple steps based on the current state of n (direction), the call could // be here instead of assigning direction for more performance if($direction) { $sizeMax = $size; $step = (int) round($step/2); $size -= $step; } else { $sizeLeast = $size; $step = $step*2; $size += $step; } // if sizemax - sizeleast is 1, then obviously the size is sizeLeast if(1 === ($sizeMax - $sizeLeast) && 1 === $step) { break; } } echo "Found: $size in $i iterations\n"; http://php.net/manual/en/ref.math.php

« previous php.notes (#169048) next »