Someone could give me an example of friendly numbers in PHP

2

Help with an example of checking friends numbers in PHP, I can not find any example or tutorial on the web

Two positive integers A and B are friendly numbers if the sum of the proper divisors of A is equal to B and the sum of the proper divisors of B is equal to A.

The divisors of a number include the unit but not the number itself.

I AM NEW IN PHP AND I TRY TO MODIFY A CODE THAT FINDS JAVA:

if($_POST)
{   
    $num1 = $_POST
    ['numero1'];
    $num2 = $_POST
    ['numero2'];

    $suma = 0;

    for($i=1;$i<$num1;$i++){

             if($num1%$i==0){

            $suma=$suma+$i;
         }
    }

    if($suma==$num2){
       $suma=0;
       for($i=1;$i<$num2;$i++){  
            if($num2%$i==0){
               $suma=$suma+$i;
           }
       }

       if($suma==$num1){
          echo 'Son Amigos';
       }else{
                 echo 'No son Amigos';
       }
    }        
    else{
              echo 'No son Amigos';
    }
}

}

    
asked by Luis Timaná 04.12.2018 в 22:40
source

1 answer

1

I was able to solve it: D

if($_POST)
{   
    $num1 = $_POST
    ['numero1'];
    $num2 = $_POST
    ['numero2'];         


            for($i = 1, $total1 = 0, $sum1 = 0; $i < $num1; $i++){
                if ($num1 % $i == 0){
                    $total1++;
                    $sum1=$sum1+$i;                 
                }                   
             }


            for($i = 1, $total2 = 0, $sum2 = 0; $i < $num2; $i++){
                if ($num2 % $i == 0){
                    $total2++;
                    $sum2=$sum2+$i;                 
                }                   
             }
            echo "La suma de divisores de $num1 es: ".$sum1." <br> La suma de divisores de $num2 es: ".$sum2;
    if ($num1 == $sum2 && $num2 == $sum1){
        echo " <br> Los Numeros son amigos!!!";
    }else{
        echo "<br> Los Numeros NO son amigos!!!";
    }
}
    
answered by 04.12.2018 в 23:06