Determine prime numbers with multplicar table

0

It remains for me to establish that the multiplication table of the prime number appears

<?php

for($i=1;$i<=100;$i++)
{
    if(primo($i))
        echo '<div style="color:green;font-weight:bold;">' .  " ".$i." es un primo ". "<br/>"   ;

    else
        echo '<div style="color:red"; font-weight:bold;>' . " ".$i." NO es número primo " . "<br/>";

    //
}


/**

 * Función que determina si un numero es primo

 * Tiene que recibir el numero a determinar si es primo o no

 * Devuelve True o False

 */

function primo($num)

{
    $cont=0;

    // Funcion que recorre todos los numero desde el 2 hasta el valor recibido

    for($i=2;$i<=$num;$i++)
    {
        if($num%$i==0)
        {
            # Si se puede dividir por algun numero mas de una vez, no es primo
            if(++$cont>1)
                return false;
        }
    }
    return true;


}

?>
    
asked by rafael 06.10.2018 в 11:14
source

3 answers

1

Here is how to find a prime number, how to make the multiplication table, the number is a story.

    <?php

for($i=1;$i<=100;$i++)
{
    if(primo($i))
        echo "El número ".$i." es primo";
    else
        echo "El número ".$i." NO es primo";
}
/**

 * Función que determina si un numero es primo

 * Tiene que recibir el numero a determinar si es primo o no

 * Devuelve True o False

 */

function primo($num)

{
    $cont=0;

    // Funcion que recorre todos los numero desde el 2 hasta el valor recibido

    for($i=2;$i<=$num;$i++)
    {
        if($num%$i==0)
        {
            # Si se puede dividir por algun numero mas de una vez, no es primo
            if(++$cont>1)
                return false;
        }
    }
    return true;

}

?>
    
answered by 06.10.2018 в 17:19
0

This function comes from a post in the English version of OS, in summary:

  • The number one is not a prime
  • The number two if it is a prime number , is the only even number that is prime.
  • If the number is divisible by two, it means that is not a cousin
  • Reviewing the odd if factors are obtained then is not a prime .
  • After these tests, the result would be a prime number.
  • .

    function isPrime($num) {
        if($num == 1)
            return false;
    
        if($num == 2)
            return true;
    
        if($num % 2 == 0) 
            return false;
    
        $ceil = ceil(sqrt($num));
        for($i = 3; $i <= $ceil; $i = $i + 2) {
            if($num % $i == 0)
                return false;
        }
    
        return true;
    }
    
        
    answered by 07.10.2018 в 03:09
    0

    Friend you have not understood that it is a prime number, the program that you have put what it does is that it tells you if the number is even or odd (but not if it is cousin or not)

    Cousin is all that number that is only divisible by the unit (which can be divided by one (all numbers are divisible by one)) and by the same

    therefore a pirmo number has 2 and only 2 divisors, except 1 which also fulfills the condition but has a single divisor

    $numero;
    $cantdedivisores=0;
    
    for($i=1;$i<=$numero;$i++){
      if($numero%$i==0){
        $cantdedivisores++;
      }
    }
    
    if(cantdedivisores==1 && cantdedivisores==2){
      echo "el numero".$numero."es primo";
    }
    

    The tables I have not understood very well, if you could explain it better, I would help you without any problem, I hope it helps you.

        
    answered by 06.10.2018 в 17:35