Cousins and multiplication tables

2

I can not get this code going. It is about listing the prime and non-prime numbers defined in a range by showing the multiplication table of those that are prime. Let's see if someone clarifies how to define the code.

<?php


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

         echo "<table border=5 px  hight:200px width= 200px cellspacing=5  cellpadding=5 display: block   >";
         echo "<tr><th> Tabla del $tabla </th></tr>";
         {   
   // generamos la tabla 
     elseif(primo($i)) 
        for($i=1; $i<=10; $i++) 
     {

        echo "<tr><td align=center>$tabla por $i </td>
              <td  align=center style=background-color:green > ". ($tabla*$i) . "</td>
             </tr>";
     }

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


}




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 07.10.2018 в 18:08
source

2 answers

2

OK, modify your code since you had a mess in the time of assembling the table and in terms of logic, I managed to optimize the function of the prime numbers and the table, you can see the code working here .

for($i=3;$i<=7;$i++){

  if(primo($i)){ 

    echo '<table border="1" width="200px" cellspacing="0"  cellpadding="0">
            <tr>
              <th style="color:green;font-weight:bold;" colspan="2"> '.$i.' es un primo.</th>
            </tr>';

    for($j=1; $j<10; $j++){
      echo '<tr>
              <td align=center>'.$i.' por '.$j.' </td>
              <td  align=center style="background-color:green" > '. ($i*$j) . '</td>
            </tr>';
    }

    echo '</table>';

  }else{

     echo '<table style="border=5px;hight:200px;" width="200px" cellspacing="5"  cellpadding="5">
             <tr>
               <td align=center>$i por $i </td>
               <td  align=center style="color:red; font-weight:bold;"> '.$i.' NO es número primo</td>
             </tr>
           </table>';

  }

}


function primo($num){

  $primo = 0;

  for ($b = 1; $b < $num; $b++) {
    if ($num % $b == 0) {
        $primo++;
    }
  }

  if ($primo >= 2) {
    return false;
  } else {
    return true;
  }

}


?>

I hope it's what you're looking for.

    
answered by 07.10.2018 / 20:00
source
2

You need to review php and html. Really.

Your code:

<?php


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

         echo "<table border=5 px  hight:200px width= 200px cellspacing=5  cellpadding=5 display: block   >";
         echo "<tr><th> Tabla del $tabla </th></tr>";
         {   
   // generamos la tabla 
     elseif(primo($i)) 
        for($i=1; $i<=10; $i++) 
     {

        echo "<tr><td align=center>$tabla por $i </td>
              <td  align=center style=background-color:green > ". ($tabla*$i) . "</td>
             </tr>";
     }

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


}




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;

}

?>

Errors:

  • All the lines where $tabla appears because it is a variable that you never define it.
  • Use i in two loops for followed. The first loop will increment i , but the second loop will also increase and when the second loop is finished, the value of i will not be the value of the first loop for but of the second .
  • HTML tags with format or style without quotes: echo "<table border=5 px hight:200px width= 200px cellspacing=5 cellpadding=5 display: block >"; and non-closed tags. You need </div> and </table> to close the div and table that you create.
  • I do not know if there are more, but when I saw that, I stopped telling. Your "fixed" code would look like this:

    <?php
        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;
        }
    
        for($i=3;$i<=7;$i++){
            if(primo($i)){
                echo "<table>";
                echo '<tr><th style="color:green"> '.$i." es primo</th></tr>";
                echo "<table border='5'>";
                echo "<tr><th> Tabla del ".$i."</th></tr>";
                // Generamos la tabla 
                for($k=0; $k<=10; $k++){
                    echo '<tr><td align="center">'.$i." por ".$k.' </td>
                        <td  align="center" style="background-color:green"> '.($k*$i). "</td></tr>";
                 }
                 echo "</table>";
            }
            else{
                echo "<table>";
                echo '<tr><th style="color:red"> '.$i." no es primo</th></tr>";
                echo "</table>";
            }
            echo "<br/>";
        }
    ?>
    

    I think the table to show if a number is prime or not instead of div, so you do not have to be assigning positions and that, and they are directly below each other.

        
    answered by 07.10.2018 в 20:05