Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,983 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,387 people online right now. Registration is fast and FREE... Join Now!




Factor/Prime Number Calculator

 
Reply to this topicStart new topic

Factor/Prime Number Calculator

coleman0121
16 Nov, 2006 - 04:39 PM
Post #1

New D.I.C Head
*

Joined: 23 Jul, 2006
Posts: 17


My Contributions
Hey everybody,
I have to make a prime number calculator.
I am doing it in php, since it is the language which i know best.
So, I got started, and have yet to finish writing the algorithm into it, but it already doesnt work. I have tried everything I know here, but it doesnt seem to work. Just prints out "1 is a factor" I see that that is $y and it I guess is not finishing my for loop. I checked and all the syntax seems correct, but I am probably wrong.

CODE
<html>
        <head><ul style= color:white;><center>Is your number prime???</center>
        <title>Prime Numbers</title>
        </head>
        <body bgcolor="black">
        <ul style=color:white;>
        <p>Enter Number<form action="prime.php" method="post">

        <?php
        print(" <p> <input type = 'text' name= 'number' size= '8'>");
        $number = $_POST['number'];
        For($y = 1; $y <= $number / 2; $y++) {
          $z = $number % $y;
          If($z ==0) {

                print("$y is a factor.");
                break;
          }
        }
        ?>
        </body>
</html>


Thanks in advanced for any suggestions you have!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Factor/Prime Number Calculator
16 Nov, 2006 - 04:43 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
You have a break command that is breaking out of the loop. The first time through, it assigns the modulus of your entered number and the value of y, which is 1, to the z variable. As there is no remainder, z is zero, it meets the condition, and breaks the loop.

Take a look at this implelemtation:
http://www.phpfreaks.com/quickcode/Prime-n...nerator/547.php
User is offlineProfile CardPM
+Quote Post

coleman0121
RE: Factor/Prime Number Calculator
16 Nov, 2006 - 05:00 PM
Post #3

New D.I.C Head
*

Joined: 23 Jul, 2006
Posts: 17


My Contributions
Ah, I get it, I used break instead of <br>
heh, well now it is working.
Thanks a lot!
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Factor/Prime Number Calculator
16 Nov, 2006 - 08:18 PM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,301



Thanked: 101 times
Dream Kudos: 1275
My Contributions
If you set y to 2 instead of 1 it won't have that problem any more. Updated code will look something like this:

CODE
<html>
        <head><ul style= color:white;><center>Is your number prime???</center>
        <title>Prime Numbers</title>
        </head>
        <body bgcolor="black">
        <ul style=color:white;>
        <p>Enter Number<form action="prime.php" method="post">

        <?php
        print(" <p> <input type = 'text' name= 'number' size= '8'>");
        $number = $_POST['number'];
        For($y = 2; $y <= $number; $y++) {
          $z = $number % $y;
          If($z ==0) {

                print("$y is a factor.");
                break;
          }
        }
        ?>
        </body>
</html>


$y is set to 1, and one is a factor of every number no matter what, so it is doing exactly what you told it to, just that it is a computer and doesn't understand what you are wanting it to get out of the process.

This post has been edited by BetaWar: 16 Nov, 2006 - 09:22 PM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 07:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month