简体   繁体   English

如何检查输入设置为LIMIT的素数

[英]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? 我的问题是,我怎样才能轮换我的代码,以便该表可以10组为动态表,最大为100? So that the input can only take increments of 10 and the max value you can enter 100? 这样输入只能以10为增量,最大值可以输入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? 我当前的代码现在设置为10列和4行,因此有没有办法使此更改(动态,我猜是?)随输入增量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>
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 %为Modulo,有关更多详细信息,请参见http://php.net/manual/en/language.operators.arithmetic.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM