Jump to content

How can I divide value by x and count how many times it is possible?


Staggan

Recommended Posts

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

 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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 :)

 

 

 

Link to comment
Share on other sites

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 ]

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.