Demonic Posted May 14, 2008 Share Posted May 14, 2008 Alright I got two numbers (numbers might change) Anyways lets say we got the number 9, I want to check say if 4 can go into nine equally. If not I want to see how many we need to add before it can be divisible by 4 evenly I got an idea actually, let me try writing a function. In the mean time post ur outcome if you know something quicker. This is what I came up with: function number_divisibility($num1,$num2) { $save = 0; if($num1 % $num2 == 0) return 0; if($num1 % $num2 != 0) { while(True) { $save++; if(($num1+$save) % $num2 == 0) return $save; } } } Quote Link to comment https://forums.phpfreaks.com/topic/105540-number-divisibility-help-trying-to-do-a-check/ Share on other sites More sharing options...
btherl Posted May 14, 2008 Share Posted May 14, 2008 The number you need to add is (4 - ($num1 % $num2)). $save = $num2 - ($num1 % $num2); Quote Link to comment https://forums.phpfreaks.com/topic/105540-number-divisibility-help-trying-to-do-a-check/#findComment-540639 Share on other sites More sharing options...
Demonic Posted May 14, 2008 Author Share Posted May 14, 2008 hmm, never thought of it like that Quote Link to comment https://forums.phpfreaks.com/topic/105540-number-divisibility-help-trying-to-do-a-check/#findComment-540646 Share on other sites More sharing options...
sasa Posted May 14, 2008 Share Posted May 14, 2008 $save = ($num1 % $num2) ? $num2 - ($num1 % $num2) : 0; Quote Link to comment https://forums.phpfreaks.com/topic/105540-number-divisibility-help-trying-to-do-a-check/#findComment-540790 Share on other sites More sharing options...
roopurt18 Posted May 14, 2008 Share Posted May 14, 2008 $save = ($tmp = ($num1 % $num2)) ? $num2 - $tmp : 0; Might as well save an operation while we're at it! Quote Link to comment https://forums.phpfreaks.com/topic/105540-number-divisibility-help-trying-to-do-a-check/#findComment-541386 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.