ivoilic Posted July 10, 2012 Share Posted July 10, 2012 Hi I relatively new to PHP and I am having trouble solving this problem: I want to take the number the user has given and then enter it in this equation 100/(2^X). I can do this easily. However I also need to apply the same equation to every number less than the given (If the user enters 4 it must by applied to 3,2,1 as well) I can generate a list of numbers using the range() function: foreach (range(0, $users_number) as $number) { echo $number; } However I can't figure out how to apply the equation to each number. The next step is to get the total sum but given the values of the the others that's pretty easy. Quote Link to comment https://forums.phpfreaks.com/topic/265457-doing-some-calculations/ Share on other sites More sharing options...
scootstah Posted July 10, 2012 Share Posted July 10, 2012 How's this? <?php $number = 4; $range = range(1, $number); $calculated = array(); foreach($range as $r) { $calculated[] = 100 / pow(2, $r); } echo array_sum($calculated); Quote Link to comment https://forums.phpfreaks.com/topic/265457-doing-some-calculations/#findComment-1360451 Share on other sites More sharing options...
requinix Posted July 10, 2012 Share Posted July 10, 2012 Yay math. A for loop can do the individual numbers, and the sum is simply 100 * (1 - 1/2^X) Hope I didn't just spoil something. Quote Link to comment https://forums.phpfreaks.com/topic/265457-doing-some-calculations/#findComment-1360456 Share on other sites More sharing options...
ivoilic Posted July 10, 2012 Author Share Posted July 10, 2012 Thanks scootstah you solution worked perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/265457-doing-some-calculations/#findComment-1360457 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.