My html document has the following form:
<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>
Edit: Using a different code now but can't get it to echo a statement no matter what I do. Anyone know how I can make it echo is a prime number or is not.
<?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;
}
}
}
?>
simple and easy to understand code, and working as well. do little changes according to your requirement, like assign input text value to $a variable and you good to go.
$a = 51;
for($i=2; $i < $a; $i++){
if($a % $i == 0){
echo "Numberis not prime.";
break;
}
else{
echo "Number is not prime.";
break;
}
}
A simple function to check is prime number:
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
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);
The output is
29 is prime number
For More Details
You may use the gmp package and correct your code because I got an output as 11 not being a prime number or use the following function as an alternative.
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;
}
More information is available at the following page.
A simple function to help you find out if a number is a prime number
<?php
function fn_prime($number) {
$i = 2; $result = TRUE;
while($i < $number) {
if(!($number%$i)) {
$result = FALSE;
}
$i++;
}
return $result;
}
?>
Related
<?php
if (isset($_GET['number'])){ // Loaded for first time?
if(isValid($_GET['number'])){
$isPrime = true;
// Please enter code here
// I have tried the codes here but idk why it cannot seem to work
$i = $_GET(['number']);
if ($number == 1) {
$isPrime = false;
return $isPrime;
}
for ($j = 2; $i <= $number/2; $i++){
if ($number % $i == 0)
$isPrime = false;
return $isPrime;
}
return $isPrime;
// End of code
if ($isPrime) {
echo "<p>".$i." is a prime number!</p>";
} else {
echo "<p>".$i." is not prime.</p>";
}
} else{
// User submitted something which is not a positive whole number
echo "<p>Please enter a positive whole number.</p>";
}
}
// check if the given number is a valid numeric value
// round() rounds a floating point value
function isValid($number) {
if(is_numeric($number) && $number > 0
&& $number == round($number, 0)) {
return true;
}
else {
return false;
}
}
?>
I have tried the above codes, however, I have encountered issues. Please help me out thank you! The codes I have tried are within the // Please enter code here to // End of code
When I run the php file the above image will be shown. The user have to type a number and it will check whether or not the user input is a prime number when user clicks on the "Go!" button.
You had many mistakes.
So I edited the whole code.
here it is:
<?php
if (isset($_GET['number'])) { // Loaded for first time?
if (isValid($_GET['number'])) {
$isPrime = true;
// Please enter code here
// I have tried the codes here but idk why it cannot seem to work
$i = $_GET['number'];
if ($i == 1) {
$isPrime = false;
}
for ($j = 2; $j <= $i / 2; $j++) {
if ($i % $j == 0)
$isPrime = false;
}
// End of code
if ($isPrime) {
echo "<p>" . $i . " is a prime number!</p>";
} else {
echo "<p>" . $i . " is not prime.</p>";
}
} else {
// User submitted something which is not a positive whole number
echo "<p>Please enter a positive whole number.</p>";
}
}
// check if the given number is a valid numeric value
// round() rounds a floating point value
function isValid($number)
{
if (is_numeric($number) && $number > 0
&& $number == round($number)) {
return true;
} else {
return false;
}
}
?>
<form action="index.php" method="get">
<input type="number" name="number">
<input type="submit" value="Go!">
</form>
I'm having trouble making a simple count work. Right now 0 is constantly displayed when I run the code and I know it's because I set count to 0. But it should be displaying the number of times "Fizz" is displayed.
I'm sure it's simple but I just can't see what!
public function __construct($firstParam, $secondParam, $firstSound = "Fizz", $secondSound = "Buzz", $numbers = 100) {
$this->firstParam = $firstParam;
$this->secondParam = $secondParam;
$this->firstSound = $firstSound;
$this->secondSound = $secondSound;
$this->numbers = $numbers;
$this->numsArray = $numsArray;
}
public function __toString() {
$count = 0;
for ($i = 0; $i < count($this->numsArray); $i++){
$val = $this->numsArray[$i];
if ($val == $this->firstSound) {
$count++;
}
}
$print = "Number of Fizzes: ".$count;
return $print;
}
public function execute() {
$this->numsArray = array();
if ($this->secondParam > $this->firstParam) {
for ($i = 1; $i <= $this->numbers; $i++){
if ($i % $this->firstParam == 0 && $i % $this->secondParam == 0) {
$this->numsArray[] = "\n".$this->firstSound.$this->secondSound."\n";
} elseif ($i % $this->firstParam == 0) {
$this->numsArray[] = "\n".$this->firstSound."\n";
} elseif ($i % $this->secondParam == 0) {
$this->numsArray[] = "\n".$this->secondSound."\n";
} else {
$this->numsArray[] = "\n".$i."\n";
}
echo $this->numsArray[$i-1];
}
} else {
echo "\n".' First Number Bigger Than Second '."\n";
}
}
In your execute you are not assigning the values to numsArray[i] also you inject new line characters that will not match the equality you just when checking $val. Also I notice you use zero index to check them and index 1 to load it. Change execute to:
for ($i = 0; $i < $this->numbers; $i++){
if ($i % $this->firstParam == 0 && $i % $this->secondParam == 0) {
$this->numsArray[i] = $this->firstSound.$this->secondSound;
} elseif ($i % $this->firstParam == 0) {
$this->numsArray[i] = $this->firstSound;
} elseif ($i % $this->secondParam == 0) {
$this->numsArray[i] = $this->secondSound;
} else {
$this->numsArray[i] = $i;
}
echo $this->numsArray[$i];
This is a better binary string comparison for php...
if (strcmp($val, $this->firstSound) == 0)
I'm triyng to catch timeout error to output some clear text to the user (like "Sorry, timeout").
So why does this example:
function shutdown() {
$a=error_get_last();
if($a==null)
echo "No errors";
else
print_r($a);
}
register_shutdown_function('shutdown');
ini_set('max_execution_time',1 );
sleep(3);
output no errors?? I'm confused about it.
Here this example looks helpful.
Thanks
Try not using sleep(), seems to work if the reason for timeout is real work:
Example
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;
}
function shutdown()
{
$a=error_get_last();
if($a==null)
echo "No errors";
else
print_r($a);
}
register_shutdown_function('shutdown');
ini_set('max_execution_time',1 );
$ps = 0;
for ($i = 0; $i < 1000000; $i++) {
if (isPrime($i)){
$ps++;
}
}
echo $ps;
here is the problem, i need to find prime numbers up to a certain number and here is my code:
$a = 56;
for($i = 2; $i<=$a; $i++)
{
if($i == 2)
{
echo "2</br>";
}
if($i == 3)
{
echo "3</br>";
}
if($i == 5)
{
echo "5</br>";
}
if($i == 7)
{
echo "7</br>";
}
for($j =3; $j <= ceil($i/2); $j = $j + 2)
{
if($i % 2 == 0)
{
break;
}
if($i % 3 == 0)
{
break;
}
if($i % 5 == 0)
{
break;
}
if($i % 7 == 0)
{
break;
}
else
{
echo "$i</br>";
break;
}
}
}
It works fine, but it kinda seems like a brute force algorithm, doesnt it? Is there any other way to do this?
Thanks for help!!!
Suppose x is the limit (till which you want prime number)..
for($n=2;$n<=$x;$n++)
{
$i=2;
while($i<=$n-1)
{
if($n % $i == 0)
break;
$i++;
}
if($i==$num)
echo $n; // $n is the prime number...
}
I had an interview today and the person asked me this question:
How do you find easily an item in a circularly sorted array
Since I didn't know the answer, I tried to find a solution. Here's what I have:
Thanks
<?php
function searchincircularsorterlist($a, $len, $num) {
$start=0;
$end=$len-1;
$mid = 0;
while($start<$end) {
$mid=$start+$end/2;
if ($num == $a[$mid]) {
return $num;
}
if($num<$a[$mid]) {
if($num<$a[$start] && $a[$start]<=$a[$start+1])
$start=$mid++;
else
$end=$mid--;
}
else {
if($num>$a[$end] && $a[$end-1]<=$a[end])
$end=$mid--;
else
$start=$mid++;
}
}
if ($start == $end && $num == $a[$start]) {
return $num;
}
return -1;
}
$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));
I am trying to work with a circularly sorted array but for some reason it does not work. What's wrong with my code?
1) learn priority of operations. You should have: $mid=($start+$end)/2; which you ended up dividing $end by 2 and then $start - the result. This is why you got an infinite loop.
2) use: $start=$mid+1; and not $start=$mid++; that will help reducing the number of loops
<?php
function searchincircularsorterlist($a, $len, $num) {
$start=0;
$end=$len-1;
$mid = 0;
while($start<$end) {
$mid=($start+$end)/2;
if ($num == $a[$mid]) {
return $num;
}
if($num<$a[$mid]) {
if($num<$a[$start] && $a[$start]<=$a[$start+1])
$start=$mid+1;
else
$end=$mid-1;
}
else {
if($num>$a[$end] && $a[$end-1]<=$a[end])
$end=$mid-1;
else
$start=$mid+1;
}
}
if ($start == $end && $num == $a[$start]) {
return $num;
}
return -1;
}
$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));