Wrox Programmer Forums
|
BOOK: Beginning PHP 5.3
This is the forum to discuss the Wrox book Beginning PHP 5.3 by Matt Doyle; ISBN: 978-0-470-41396-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 5.3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2016, 10:40 AM
Registered User
 
Join Date: Jul 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 4 Exercise 1

The solution in the book to exercise 4.1 is a hard coded listing of prime numbers.
I'm trying to put together a function that determines if the number is prime and then echos "Yes" or "No".
Here is where I'm hosting the exercise:

http://ajjpreble.com/learning-php/exercise4-1.php

And Here is the code I've used:

PHP Code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="common.css" />
<title>Exercise 4.1</title>
<style type="text/css">
th { text-align: left; background-color: #999; }
th, td { padding:0.4em;}
tr.alt td { background: #ddd; }
</style>
</head>

<table>
<tr>
<th>Number</th>
<th>Parity</th>
<th>Prime</th>
</tr>


<?php

$iterations 
10;

for (
$num 1$num <= $iterations$num++ ) 
{

?>

<tr<?php if ( $num != ) echo ' class="alt" ' ?>>
<td><?php echo $num ?></td>
<td>
<?php 
    
if ($num%== ) {
        echo 
"Even";
        }
    else {
        echo 
"Odd";
        }
    
?>

</td>
<td> 
<?php 
//1 is not prime. See: [url]http://en.wikipedia.org/wiki/Prime_number#Primality_of_one[/url]
   
if($num == 1)
        echo 
"No";
         
//2 is prime (the only even number that is prime)
    
if($num == 2)
        echo 
"Yes";
        
 
/**
     * if the number is divisible by two, then it's not prime and it's no longer
     * needed to check other even numbers
     */
    
if($num !=&& $num == 0) {
        echo 
"No";
    }        
$primeIterations ceil(sqrt($num));
for (
$divisor $divisor <= $primeIterations$divisor $divisor+) {
    if  ( 
$num%$divisor == 0) echo "Yes";
    else echo 
"No";
}
?>

</td>
</tr>
<?php ?>
</table>


<body>
</body>
</html>
Could anyone give me some input?
Thank you





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exercise 2 and 3 Chapter 5 frdata BOOK: Beginning Database Design Solutions ISBN: 978-0-470-38549-4 7 May 5th, 2014 12:05 PM
Chapter 2 Exercise 2 rolndd BOOK: Ivor Horton's Beginning Visual C++ 2010 2 July 12th, 2011 01:58 PM
Chapter 2 Exercise #5 yowzer14 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 August 26th, 2010 12:33 AM
Chapter 5 exercise 3 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 27th, 2009 02:41 PM
Chapter 4, Exercise 3 DRAYKKO BOOK: Beginning Java 2 3 July 9th, 2004 02:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.