在PHP中检查素数

时间:2014-04-13 18:37:17

标签: php

我的html文档格式如下:

<form action="PrimeNumber.php" method="post">
Enter a number to determine if it is a prime number: <input type="text" name="numb" size="10">
<input type="submit" value="Check for Primeness">
</form>

编辑:现在使用不同的代码,但无论我做什么,都无法回复语句。任何人都知道如何使它回声是一个素数或不是。

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['numb'])) {
        $num = $_POST['numb'];

        function isPrime($num) {
            if($num == 1) {
                return false;
            }

            if($num == 2) {
                return true;
            }

            if($num % 2 == 0) {
                return false;
            }

            for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
                if($num % $i == 0)
                    return false;
            }
            return true;
        }
    }
}
?>

5 个答案:

答案 0 :(得分:1)

一个简单的功能就是素数:

sap.ui.define(["sap/m/Page", "sap/m/List", "sap/m/Toolbar", "sap/m/SearchField", "sap/m/Label", "sap/m/Text"], function(Page, List, Toolbar, SearchField, Label, Text) {
  "use strict";
  return sap.ui.jsview("sap.m.sample.ListSelectionSearch.View", {
    getControllerName: function() {
      return "sap.m.sample.ListSelectionSearch.List";
    },
    createContent: function(oController) {
      var oToolbar = new Toolbar({
        visible: true,
        content: [
          new SearchField({
            liveChange: function() {
              oController.onSearch();
            },
            width: "100%"
          })
        ]
      });
      var oInfoToolbar = new Toolbar({
        content: new Toolbar(this.createId("idInfoToolbar"), {
          visible: true,
          content: new Text({
            text: "Label Text"
          })
        })
      });

      var oList = new List(this.createId("idList"), {
        mode: "MultiSelect",
        includeItemInSelection: true,
        infoToolbar: oInfoToolbar
      });

      var oPage = new Page(this.createId("oPageId"), {
        height: "100%",
        title: "Page Title",
        showHeader: true,
        showSubHeader: true,
        headerContent: oToolbar,
        content: [oList]
      });
      var app = new sap.m.App();
      app.addPage(oPage);
      app.placeAt("content");
      return app;
    }

  });
});

//in Controller 
sap.ui.define(["sap/m/StandardListItem", "sap/ui/model/json/JSONModel"], function(StandardListItem, JSONModel) {
  "use strict";
  var oData = {

    "ProductCollection": [{
      "titleId": 0,
      "Name": "Olayinka Otuniyi",
      "ProductId": "001",
      "ProductPicUrl": "sap-icon://competitor"
    }, {
      "titleId": 1,
      "Name": "Maria Anders",
      "ProductId": "002",
      "ProductPicUrl": "sap-icon://badge"
    }, {
      "titleId": 2,
      "Name": "Ana Trujillo",
      "ProductId": "003",
      "ProductPicUrl": "sap-icon://broken-link"
    }, {
      "titleId": 3,
      "Name": "Thomas Hardy",
      "ProductId": "004",
      "ProductPicUrl": "sap-icon://create"
    }, {
      "titleId": 4,
      "Name": "Christina Berglund",
      "ProductId": "005",
      "ProductPicUrl": "sap-icon://pending"
    }, {
      "titleId": 5,
      "Name": "Hanna Moos",
      "ProductId": "006",
      "ProductPicUrl": "sap-icon://decision"
    }, {
      "titleId": 6,
      "Name": "Martín Sommer",
      "ProductId": "007",
      "ProductPicUrl": "sap-icon://process"
    }, {
      "titleId": 7,
      "Name": "Laurence Lebihans",
      "ProductId": "008",
      "ProductPicUrl": "sap-icon://accept"
    }, {
      "titleId": 8,
      "Name": "Elizabeth Lincoln",
      "ProductId": "009",
      "ProductPicUrl": "sap-icon://alert"
    }]

  };
  return sap.ui.controller("sap.m.sample.ListSelectionSearch.List", {

    //		onInit: function() {
    //		},		
    onAfterRendering: function() {
      var oModel = new JSONModel(oData);
      this.getView().setModel(oModel, "products");

      var oTemplate = new StandardListItem({
        title: "{products>Name}",
        description: "{products>ProductId}",
        icon: "{products>ProductPicUrl}",
        iconDensityAware: false,
        iconInset: false,
        type: "Active"
      });
      oTemplate.attachPress(this.onSelectionChange, this);
      this.getView().byId("idList").bindItems({
        path: "/ProductCollection",
        template: oTemplate,
        model: "products"
      });
    },
    onSearch: function() {
      console.log("Searching");
    },
    onSelectionChange: function() {
      console.log("changing Selection");
    }
  });
});

答案 1 :(得分:1)

function checkPrime($num){
      $isPrime = true;
    
    for($i = 2; $i <= sqrt($num); $i++)
    {
        if($num % $i == 0) 
        {
          $isPrime = false;
        }
    }
    if($isPrime == true)
    {
        return "$num is prime number";
    }else{
        return "$num  is not prime number";
    }
}

echo checkPrime(29); 

输出为

29 is prime number

For More Details

答案 2 :(得分:0)

您可以使用gmp包并更正您的代码,因为我得到的输出为11不是素数或使用以下函数作为替代。

function isPrime($num) {
    if($num == 1) {
        return false;
    }

    if($num == 2) {
        return true;
    }

    if($num % 2 == 0) {
        return false;
    }

    for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {

        if($num % $i == 0)

            return false;
    }
    return true;
}

以下page提供了更多信息。

答案 3 :(得分:0)

一个简单的函数,可帮助您确定数字是否为素数

<?php 
     function fn_prime($number) {
        $i = 2; $result = TRUE;
        while($i < $number) {
            if(!($number%$i)) {
                $result = FALSE;
            }
            $i++;
        }
        return $result;
     }
?>

答案 4 :(得分:0)

简单易懂的代码,并且工作正常。根据您的要求进行少量更改,例如将输入文本值分配给$ a变量,然后您就可以了。

$a = 51;
for($i=2; $i < $a; $i++){
    if($a % $i == 0){
        echo "Numberis not prime.";
        break;
    }
    else{
        echo "Number is not prime.";
        break;
    }
}