Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Beginning PHP 5.3
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 5th, 2016, 10:40 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 05:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.