How to check prime number with an input set as LIMIT

So i have the code to check for Prime numbers with value that is inputted. My question is, how can i turn my code so that the table can be dynamic in sets of 10 and the max is 100? So that the input can only take increments of 10 and the max value you can enter 100?

My current code right now is set to 10 columns and 4 rows so is there a way to make this change (dynamic i guess?) to change with the input increments of 10?

Here is my code:

<form method="post" action="">
     <table border="2" width="1180px">
        <thead>
            <center>
                Number Chart</center>

        </thead>

    <?php
    error_reporting(0);

    function isPrime($n)
    {
    if ($n == 1) return false;
    if ($n == 2) return true; 
    if ($n % 2 == 0)
        {
        return false;
        }

    $i = 2;
    for ($i = 2; $i < $n; $i++)
        {
        if ($n % $i == 0)
            {
            return false;
            }
        }

    return true;
    }


    if (isset($_POST['value']) && $_POST['value'] != 0)
    {
    /* @var $start type */
    $start = $_POST['value'];
    }
    else
    {
    $start = 1;
    }


    $n_cols = 10;
    $n_rows = 5;

    for ($i = 0; $i < $n_rows; $i++) // 
    {
    $col = '';
    for ($j = 0; $j < $n_cols; $j++)
        {
        $number = ($start - $i - ($j * $n_cols));

        if (isPrime($number) == true)
            {
            //if prime color it red
            $col.= '<th style="color:red">' . ($start - $j - ($i * $n_cols)) . 
    '</th>';
            }
          else
            {
            $col.= '<th>' . ($start - $j - ($i * $n_cols)) . '</th>';
            }
        }

    $out.= '<tr>' . $col . $row . '</tr>';
    }

    echo $out;
    ?>

     <tr>   
            <thead colspan=10>
                    <center>
                        <label for="input">Enter Limit:</label>
                        <input type="text" name="value" style="width: 60px">
                        <input type="submit" name="submit" value="Submit">
                    </center>
            </thead>
        </tr>
    </table>
    </form>

Upvotes: 0

Views: 196

Answers (1)

user8011997
user8011997

Reputation:

if (($_POST['value'] % 10) != 0 || $_POST['value'] >100 ){ 

echo 'you must enter a valid value';

}else{
  //all the code you want to run only if the number posted is valid
}

the % is Modulo, see http://php.net/manual/en/language.operators.arithmetic.php for more details

Upvotes: 1

Related Questions