note 85735 added to ref.math

From: Date: Mon, 15 Sep 2008 16:27:30 +0000
Subject: note 85735 added to ref.math
Groups: php.notes 
Request: Send a blank email to php-notes+get-144423@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. // 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"; ---- Server IP: 208.69.120.35 Probable Submitter: 64.202.160.65 ---- Manual Page -- http://www.php.net/manual/en/ref.math.php Edit -- https://master.php.net/note/edit/85735 Del: integrated -- https://master.php.net/note/delete/85735/integrated Del: useless -- https://master.php.net/note/delete/85735/useless Del: bad code -- https://master.php.net/note/delete/85735/bad+code Del: spam -- https://master.php.net/note/delete/85735/spam Del: non-english -- https://master.php.net/note/delete/85735/non-english Del: in docs -- https://master.php.net/note/delete/85735/in+docs Del: other reasons-- https://master.php.net/note/delete/85735 Reject -- https://master.php.net/note/reject/85735 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144423) next »