Question

Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all...

Using PHP:

  1. Write a function that:
  • Given a numerical parameter in input, computes and prints all the prime numbers up to that value.

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"

  1. Write a "tester function" that:
  • Given a specific subset of inputs, tests the function described above (point 1).

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
   ...

Homework Answers

Answer #1

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>

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called sum_half that takes as input a square matrix A and computes the...
Write a function called sum_half that takes as input a square matrix A and computes the sum of its elements that are in the upper right triangular part of A, that is, elements in the diagonal and elements that are to the right of it. For example, if the input is [1 2 3; 4 5 6; 7 8 9], then the function would return 26. (That is, 1+2+3+5+6+9) Note, the function triu is not allowed. Please write as you...
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
In C programming language write a function that takes an integer n as input and prints...
In C programming language write a function that takes an integer n as input and prints the following pattern on the screen: 1 (n times) 2 (n-1 times) .n (1 time) For example, if n was 5, the function should print 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
PHP Question: _____________________________________________ Write the PHP code to list out all of the dates of the...
PHP Question: _____________________________________________ Write the PHP code to list out all of the dates of the current month and assign them to their given days. As an example, for the month of October 2020, the output should look something like this: October 2020       Monday: 5, 12, 19, 26 Tuesday: 6, 13, 20, 27 Wednesday: 7, 14, 21, 28 Thursday: 1, 8, 15, 22, 29 Friday: 2, 9, 16, 23, 30 Saturday: 3, 10, 17, 24, 31 Sunday: 4,...
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Python. create a function that takes a string as a parameter and prints out the following...
Python. create a function that takes a string as a parameter and prints out the following details as the example output below. If the string is "Hello, my name is Starling, Clarice. My, my, my, nice to meet you." The output would be: Unique words: 10 Number of commas: 5 Number of periods: 2 Number of sentences: 2 (HINT: Use the following string methods: split, lower, count, replace, format, and use the following functions: set, len. You may need to...
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Be able to write Python programming of a factorial of an integer (given input parameters, output...
Be able to write Python programming of a factorial of an integer (given input parameters, output characteristics and expected output behavior, do not use internal python functions for factorials), recall for example that: 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and to continue asking unless user says to stop NOT USING FACTORIAL FUNCTION
PLEASE HURRY, will upvote if correct python Write a method that prints characters using the following...
PLEASE HURRY, will upvote if correct python Write a method that prints characters using the following header: def printChars(ch1,ch2): this function prints out the characters between ch1 and ch2 inclusive. Assumption is that ch2 is always bigger than c1 and both lower cases. also Write a function that accepts a list as an argument and prints the number of occurrence of each item def itemOccurence(myList): i.e. input ("Hello",123, 342, 123, "Hello",123) output: 2 times "Hello" 3 times 123 1 time...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...