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; } } } 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); 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 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; 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! 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
Archived
This topic is now archived and is closed to further replies.