rvdb86 Posted February 9, 2009 Share Posted February 9, 2009 Hi, i am trying to perform a mathematical check on a number and was hoping some one could help me out! let me explain the situation. the user enters a 9 digit number - i have form validation that ensures its a number and 9 digits long i then need to x the first number by 1, the 2nd number by 2, the 3rd number by 1, the 4th number by 2 etc... the sum of the previous step should be exactly devisible by 11 Example: Number: 1 2 3 4 5 6 7 8 9 multiply: 1 2 1 2 1 2 1 2 1 result1: 1 4 3 8 5 1 7 1 9 !! -- if the result should return only 1 digit, if the result is => 10 result = 1 result2: 1+4+3+8+5+1+7+1+9 = 39 finally i need to check that the sum of result2 (39) is exactly devisible by 11 if any one know of a function that could help me, or tell me how i can count through the int to multiply by 1 or 2 and how to check if a number is exactly devisible by 11 i would very much appreciate it. thank you in advance to anyone who takes the time to look at this! Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/ Share on other sites More sharing options...
peuge Posted February 9, 2009 Share Posted February 9, 2009 First off explode the number into an array $nums = explode($number); Then you loop through the array and check if each number is divisible by two, and if it is multiply by two. $result = 0; for ($i=0;$i <=9; $i++){ if ($nums[$i] % $divideby == 0){ $nums[$i] = $nums * 2; } if ($nums[$i] >=10){ $nums[$i] = 1; } $result = $result + $nums[$i]; } Havent tested it but you get the idea Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758034 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 ok thanks for the suggestion im going to try it out now Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758040 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 the explode() function is returning an error that it is not being used in the correct way. I looked into the explode() function on php.net and the instructions there show that there need to be a delimiter between the numbers. how can i break the 9 digit number into an array without having delimiters? Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758053 Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Share Posted February 9, 2009 <?php $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput){ $sum = 0; for($count = 0; $count < 9; $count++){ $currentVal = $userInput[$count]; if($count%2 == 1){ $toAdd = $currentVal * 2; if($toAdd > 9){$toAdd = 1;} $sum += $toAdd; //echo "ODD current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; }else{ $toAdd = $currentVal * 1; if($toAdd > 9){$toAdd = 1;} $sum += $toAdd; //echo "EVEN current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; } } if($sum%11 == 0){ echo $userInput . ": Sum's to " . $sum . ": is divisible by 11"; }else{ echo $userInput . ": Sum's to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput); ?> I think that should do it all, i havent fully tested it, buy i think its ok Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758060 Share on other sites More sharing options...
Mark Baker Posted February 9, 2009 Share Posted February 9, 2009 how can i break the 9 digit number into an array without having delimiters?Usr str_split() rather than explode() Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758078 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 thank you so much to everyone who has helped me out! Where would i edit the script that anndy_b42 so kindly posted so that if the number being multiplied by either 1 or 2 >= 10 it would add these to gether. for example: 7*2 = 14 so the result i want is 1+4 = 5 Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758094 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 i think it is this part of the code i need to change: if($toAdd > 9){$toAdd = 1;} $sum += $toAdd; i just don't know how to add the two numbers together would i use str_split() on the result and then do something like $toAdd[1] + $toAdd[2] or am i on the wrong track? Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758099 Share on other sites More sharing options...
sasa Posted February 9, 2009 Share Posted February 9, 2009 <?php function chek_sum($num){ $sum = 0; $mult = 1; $num = str_split(trim($num)); if (count($num) != 9) return false; foreach ($num as $n) { $n *= $mult; $mult = 3 - $mult; $sum += (int) ($n/10) + $n % 10; } return $sum % 11 == 0; } $number = '123456786'; if(chek_sum($number)) echo 'OK'; else echo 'no'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758103 Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Share Posted February 9, 2009 thank you so much to everyone who has helped me out! Where would i edit the script that anndy_b42 so kindly posted so that if the number being multiplied by either 1 or 2 >= 10 it would add these to gether. for example: 7*2 = 14 so the result i want is 1+4 = 5 I think this will do it <?php $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput){ $sum = 0; for($count = 0; $count < 9; $count++){ $currentVal = $userInput[$count]; if($count%2 == 1){ $toAdd = $currentVal * 2; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; //echo "ODD current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; }else{ $toAdd = $currentVal * 1; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; //echo "EVEN current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; } } if($sum%11 == 0){ echo $userInput . ": Sum's to " . $sum . ": is divisible by 11"; }else{ echo $userInput . ": Sum's to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758112 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 guys thank you so much! please know that i appreciate the time you took to help me, and hopefully with my new knowledge who knows maybe i will be able to help someone like you guys helped me out! great forum, and great people! Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758113 Share on other sites More sharing options...
Mark Baker Posted February 9, 2009 Share Posted February 9, 2009 Can be simplified further: $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput) { $sum = 0; for($count = 0; $count < 9; $count++) { $currentVal = $userInput{$count}; if ($count%2 == 1) { $sum += array_sum(str_split($currentVal * 2)); } else { $sum += $currentVal; } } if (($sum % 11) == 0) { echo $userInput . ": Sums to " . $sum . ": is divisible by 11"; } else { echo $userInput . ": Sums to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput); Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758116 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 hey guys, i know i marked this topic as solved, but maybe some one could help me solve the following: the code above is for a known amount of numbers (9). How would change the code for an unknown amount of numbers between 11-19 and the 1,2,1,2 numbering has to start from right to left. Example: 13 numbers: 1 2 3 4 5 6 7 8 9 0 1 2 3 1 2 1 2 1 2 1 2 1 2 1 2 1 12 numbers: 1 2 3 4 5 6 7 8 9 0 1 2 2 1 2 1 2 1 2 1 2 1 2 1 Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758147 Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Share Posted February 9, 2009 <?php $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput, $leftToRight){ // $leftToRight is a boolean variable $sum = 0; $count = 0; if($leftToRight){ $oddMultiplier = 1; $evenMultipler = 2; }else{ $oddMultiplier = 2; $evenMultipler = 1; } while($userInput[$count] != ""){ $currentVal = $userInput[$count]; if($count%2 == 1){ $toAdd = $currentVal * $evenMultiplier; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; echo "ODD current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; }else{ $toAdd = $currentVal * $oddMultiplier; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; echo "EVEN current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; } $count++; } if($sum%11 == 0){ echo $userInput . ": Sum's to " . $sum . ": is divisible by 11"; }else{ echo $userInput . ": Sum's to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput, true); checkDivisibleByEleven($userInput, false); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758152 Share on other sites More sharing options...
Mark Baker Posted February 9, 2009 Share Posted February 9, 2009 $userInput = "1234567890123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput) { $sum = 0; $userInput = strrev($userInput); $strLength = strlen($userInput); for($count = 0; $count < $strLength; $count++) { $currentVal = $userInput{$count}; if ($count%2 == 1) { $sum += array_sum(str_split($currentVal * 2)); } else { $sum += $currentVal; } } if (($sum % 11) == 0) { echo $userInput . ": Sums to " . $sum . ": is divisible by 11"; } else { echo $userInput . ": Sums to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput); Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758176 Share on other sites More sharing options...
rvdb86 Posted February 9, 2009 Author Share Posted February 9, 2009 hey andy_b42, thanks for your help but it didnt seem to work no worries Mark Bakers script worked 100% thanks soo much! u guys are life savers! Quote Link to comment https://forums.phpfreaks.com/topic/144462-solved-please-help-with-mathematical-validation/#findComment-758185 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.