Jump to content

Recommended Posts

Write a php script that will accept a positive integer, if it is even, divide it by 2 and if it is odd, multiply it by 3 and add 1. Repeat the process until the value is 1, printing out each value. Finally print out how many of these operations you performed. If the input value is less than 1, print error and perform an exit();

 

For example:

Initial value: 9

Next value: 28

Next value: 14

Next value: 7

Next value: 22

Next value: 11

Next value: 34

Next value: 17

Next value: 52

Next value: 26

Next value: 13

Next value: 40

Next value: 20

Next value: 10

Next value: 5

Next value: 16

Next value: 8

Next value: 4

Next value:2

Final value:1

Number of steps: 19

 

 

Could you please modify this code?

<?php
$integer = intval($_GET['integer']);
if ($integer < 1) {
    echo "Please Enter a Positive Integer";
} else {
    echo "Entered Number is: ".$integer."<br />";
    while($integer != 1 AND $integer >= 1) {
        $modulus = $integer%2;    
        if ($modulus == 0) {
            $integer = $integer/2;
            echo $integer."<br />";
        } else {
            $integer = $integer*3;
            $integer = $integer+1;
            echo $integer."<br />";    
            $integer = $integer/2;;
                echo $integer."<br />";    
        }
    }
}
?>

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/85630-solved-counting-variables/
Share on other sites

$int = intval($_GET['integer']);
if ($int < 1) {
    echo "Please enter a positive integer";
} else {
    echo "Entered number is: ".$int."<br />";
$i = 0;
    while($int != 1 AND $int >= 1) {
        $modulus = $int%2;    
        if ($modulus == 0) {
            $int = $int/2;
            $i++;
            echo $i . " ". $int."<br />";
        } else {
            $int = $int*3;
            $int = $int+1;
            echo $i . " ". $int."<br />";    
		$i++;
            $int = $int/2;
            $i++;
            echo $i . " ". $int."<br />";    
        }
     }
    echo "Number of steps: $i";
}

 

Hopefully what you want?

            echo $integer."
";
        } else {
            $integer = $integer*3;
            $integer = $integer+1;
            echo $integer."
";   
            //$integer = $integer/2;
            //    echo $integer."
";
        }

 

remove the commented lines in your code

$int = intval($_GET['integer']);
if ($int < 1) {
    echo "Please enter a positive integer";
} else {
    echo "Entered number is: ".$int."<br />";
$i = 0;
    while($int != 1 AND $int >= 1) {
        $modulus = $int%2;    
        if ($modulus == 0) {
            $int = $int/2;
            $i++;
            echo $i . " ". $int."<br />";
        } else {
            $int = $int*3;
            $int = $int+1;
            echo $i . " ". $int."<br />";    
		$i++;
            $int = $int/2;
            $i++;
            echo $i . " ". $int."<br />";    
        }
     }
    echo "Number of steps: $i";
}

 

Hopefully what you want?

thank you very much

$int = intval($_GET['integer']);
if ($int < 1) {
    echo "Please enter a positive integer";
} else {
    echo "Entered number is: ".$int."<br />";
$i = 0;
    while($int != 1 AND $int >= 1) {
        $modulus = $int%2;    
        if ($modulus == 0) {
            $int = $int/2;
            $i++;
            echo $i . " ". $int."<br />";
        } else {
            $int = $int*3;
            $int = $int+1;
            echo $i . " ". $int."<br />";    
		$i++;
            $int = $int/2;
            $i++;
            echo $i . " ". $int."<br />";    
        }
     }
    echo "Number of steps: $i";
}

 

Hopefully what you want?

thank you very much

 

Please remember to press the "Solved" button to finish up with this thread, if you're happy with your solution. Thankyou. :)

Assuming the assigment has been handed in now (twostar's step numbers were a bit erratic)

 

<?php
if (isset($_GET['number']) && $_GET['number'] > 0)
{
    for ($i=0, $n = intval($_GET['number']); $n > 1; $n = ($n%2) ? $n*3+1 : $n/2, $i++)
    {
        echo $i ? sprintf('%02d Next',$i) : 'Initial', " value : $n<br/>";
    }
    echo "$i Final value : $n<br/>Number of steps : $i";
}
?>
<form>
       <hr/>
       Enter a positive integer<br/>
       <input type="text" name="number" size="5">
       <input type="submit" name="sub" value="Submit">
</form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.