Jump to content

[SOLVED] Please help with Mathematical Validation


rvdb86

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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';
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

<?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);

?>

Link to comment
Share on other sites

$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);

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.