Staggan Posted January 10, 2012 Share Posted January 10, 2012 Not sure my question is clear.. hehe I have value X and want to know how many times I can divide it by y for example: 11 / 2 Will give me: 11 / 2 = 5 (ignore remainder) 5 / 2 = 2 (ignore remainder) 2 / 2 = 1 (ignore remainder) So 11 / 2 works 3 times Any ideas? I am using this to work out where my value falls in terms of power of 2 in a sequence Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/ Share on other sites More sharing options...
QuickOldCar Posted January 10, 2012 Share Posted January 10, 2012 Using floor() is what were after <?php function divideCount($x,$y){ $value = floor($x/$y); return $value; } echo divideCount(11,2)."<br />"; echo divideCount(5,2)."<br />"; echo divideCount(2,2)."<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306159 Share on other sites More sharing options...
Maq Posted January 10, 2012 Share Posted January 10, 2012 yep ^ Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306161 Share on other sites More sharing options...
salathe Posted January 10, 2012 Share Posted January 10, 2012 Skipped mathematics at school? Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306162 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 ... I am using this to work out where my value falls in terms of power of 2 in a sequence Thanks Just work out the floor of the square root??? <?php $result = floor(sqrt(x))); echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306165 Share on other sites More sharing options...
QuickOldCar Posted January 10, 2012 Share Posted January 10, 2012 $result = floor(sqrt(11)); echo $result; if x was 11, the result would be 3, so no good Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306167 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 $result = floor(sqrt(11)); echo $result; if x was 11, the result would be 3, so no good How not good? ...So 11 / 2 works 3 times... Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306171 Share on other sites More sharing options...
QuickOldCar Posted January 10, 2012 Share Posted January 10, 2012 $result = floor(sqrt(11)); echo $result; if x was 11, the result would be 3, so no good How not good? ...So 11 / 2 works 3 times... 2 goes into 11 5 times Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306172 Share on other sites More sharing options...
Psycho Posted January 10, 2012 Share Posted January 10, 2012 No need for loops. I believe you are wanting the log() using a base of 2 (and of course using floor). function dividedByCount($number, $base) { return floor(log($number, $base)); // <== SOLUTION } //Unit test for($i=1; $i<=36; $i++) { $count = dividedByCount($i, 2); echo "$i : $count <br>\n"; } Output of the unit test: 1 : 0 2 : 1 3 : 1 4 : 2 5 : 2 6 : 2 7 : 2 8 : 3 9 : 3 10 : 3 11 : 3 12 : 3 13 : 3 14 : 3 15 : 3 16 : 4 17 : 4 18 : 4 19 : 4 20 : 4 21 : 4 22 : 4 23 : 4 24 : 4 25 : 4 26 : 4 27 : 4 28 : 4 29 : 4 30 : 4 31 : 4 32 : 5 33 : 5 34 : 5 35 : 5 36 : 5 Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306176 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 yeah, but the OP is only trying to get the neerest square that can be used without going over the original number...either that or I totally missread the post (it's been a long day) Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306177 Share on other sites More sharing options...
QuickOldCar Posted January 10, 2012 Share Posted January 10, 2012 ahh, yeah I missed this part. So 11 / 2 works 3 times I am using this to work out where my value falls in terms of power of 2 in a sequence Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306180 Share on other sites More sharing options...
Staggan Posted January 10, 2012 Author Share Posted January 10, 2012 Wow, lot of answers... Yes, basically I am looking for the highest power 2 without going over my original number.... I just did this... while ($result !== 1){ $count += 1; $result = (int)($result / 2); } Not pretty, but works.... I'll look at some of the other suggested ideas to clean it up Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306181 Share on other sites More sharing options...
Psycho Posted January 10, 2012 Share Posted January 10, 2012 yeah, but the OP is only trying to get the neerest square that can be used without going over the original number...either that or I totally missread the post (it's been a long day) The OP wanted to know how many times he can divide a number by another number - he only gave an example of 2. But, even if it was only 2 square root has no bearing on this. The square root of 36 is 6, but you cant divide that by two 6 times. (1) 36 / 2 = 18, (2) 18 / 2 = 9, (3) 9 / 2 = 4.5, (4) 4 / 2 = 2, (5) 2 / 2 = 1 I only did some rudimentary validation, but I believe my solution will give the exact results. EDIT: Just to be clear, if we are wanting to know how many times a number X can be divided by two we aren't looking for the square root of the number [ X ^ 2 <= Y ], we are solving for [ 2 ^ X <= Y ] Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306182 Share on other sites More sharing options...
Psycho Posted January 10, 2012 Share Posted January 10, 2012 Wow, lot of answers... Yes, basically I am looking for the highest power 2 without going over my original number.... I just did this... while ($result !== 1){ $count += 1; $result = (int)($result / 2); } Not pretty, but works.... Please look at the solution I provided. You don't need a loop. The opposite of a "power" is the "log". The function I provided should get you exactly the result you want with a single line (and no loop). function dividedByCount($number, $base) { return floor(log($number, $base)); // <== SOLUTION } Quote Link to comment https://forums.phpfreaks.com/topic/254733-how-can-i-divide-value-by-x-and-count-how-many-times-it-is-possible/#findComment-1306184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.