Using PHP:
Example:
If input parameter is 10, output is "2, 3, 5, 7"
If input parameter is 100, output is "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"
Example, in pseudo code:
tester_function: is output from prime_function(10) equal to "2, 3, 5, 7"? If yes, test passed is output from prime_function(0) equal to ""? If yes, test passed ...
1.
Required Function:
<?php
//Define the function.
function print_prime($num)
{
//Check for valid input.
if ($num <= 1)
{
echo "no possible primes are present upto number ".$num;
}
else
{
//Create the array to store the prime number.
$prime_array = array_fill(0, $num+1, true);
//Iterate from 2 to entered number.
for ($i = 2; $i*$i <= $num; $i++)
{
//Check for the prime number and print it.
if ($prime_array[$i] == true)
{
for ($j = $i*$i; $j <= $num; $j += $i)
$prime_array[$j] = false;
}
}
for ($i = 2; $i <= $num; $i++)
if ($prime_array[$i])
echo $i." ";
}
}
?>
Sample Implementation:
Screenshot of the Code:
index.php:
prime.php:
Sample Output:
Code to Copy:
index.php:
<html>
<head>
<title>Prime Number Function</title>
</head>
<body>
<form action ="prime.php" method = "post">
Enter the number: <input type ="text" name = "number">
<input type="submit">
<br>
</body>
</html>
prime.php:
<html>
<body>
<?php
//Define the function.
function print_prime($num)
{
//Check for valid input.
if ($num <= 1)
{
echo "no possible primes are present upto number ".$num;
}
else
{
//Create the array to store the prime number.
$prime_array = array_fill(0, $num+1, true);
//Iterate from 2 to entered number.
for ($i = 2; $i*$i <= $num; $i++)
{
//Check for the prime number and print it.
if ($prime_array[$i] == true)
{
for ($j = $i*$i; $j <= $num; $j += $i)
$prime_array[$j] = false;
}
}
for ($i = 2; $i <= $num; $i++)
if ($prime_array[$i])
echo $i." ";
}
}
//Get the input from the form created
//in index.php page.
$n = (int)$_POST["number"];
//Function call.
print_prime($n);
?>
</body>
</html>
2.
Required Function:
<?php
//Define the tester function
function tester_function()
{
//Get the input from the form created
//in index.php page.
$input = 10;
//Display the message
echo "prime numbers upto given number " .$input." are : "." ";
//call the prime function to print the prime numbers.
print_prime($input);
}
//Call the tester function.
tester_function();
?>
Sample Implementation:
Screenshot of the Code:
index.php:
prime.php:
Sample Output:
Code to Copy:
index.php:
<html>
<head>
<title>Prime Number Function</title>
</head>
<body>
<form action ="prime.php" method = "post">
Enter the number: <input type ="text" name = "number">
<input type="submit">
<br>
</body>
</html>
prime.php:
<html>
<body>
<?php
//Define the function.
function print_prime($num)
{
//Check for valid input.
if ($num <= 1)
{ echo "no possible primes are present upto number ".$num;
}
else
{
//Create the array to store the prime number.
$prime_array = array_fill(0, $num+1, true);
//Iterate from 2 to entered number.
for ($i = 2; $i*$i <= $num; $i++)
{
//Check for the prime number and print it.
if ($prime_array[$i] == true)
{
for ($j = $i*$i; $j <= $num; $j += $i)
$prime_array[$j] = false;
}
}
for ($i = 2; $i <= $num; $i++)
if ($prime_array[$i])
echo $i." ";
}
}
//Define the tester function
function tester_function()
{
//Get the input from the form created
//in index.php page.
$input = (int)$_POST["number"];
//Display the message
echo "prime numbers upto given number " .$input." are : "." ";
//call the prime function to print the prime numbers.
print_prime($input);
}
//Call the tester function.
tester_function();
?>
</body>
</html>
Get Answers For Free
Most questions answered within 1 hours.