dvayne Posted January 12, 2008 Share Posted January 12, 2008 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 More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 $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? Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-436986 Share on other sites More sharing options...
Zane Posted January 12, 2008 Share Posted January 12, 2008 echo $integer." "; } else { $integer = $integer*3; $integer = $integer+1; echo $integer." "; //$integer = $integer/2; // echo $integer." "; } remove the commented lines in your code Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-436992 Share on other sites More sharing options...
dvayne Posted January 12, 2008 Author Share Posted January 12, 2008 $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 Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-437155 Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 $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. Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-437163 Share on other sites More sharing options...
Barand Posted January 12, 2008 Share Posted January 12, 2008 Looks like someone wants his homework for the weekend done for him Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-437308 Share on other sites More sharing options...
nikefido Posted January 12, 2008 Share Posted January 12, 2008 i wish my school had a php class do your own work, son! Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-437313 Share on other sites More sharing options...
Zane Posted January 13, 2008 Share Posted January 13, 2008 Looks like someone wants his homework for the weekend done for him indeed Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-437700 Share on other sites More sharing options...
dvayne Posted January 13, 2008 Author Share Posted January 13, 2008 thanks again twostars, topic solved. Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-437707 Share on other sites More sharing options...
Barand Posted January 15, 2008 Share Posted January 15, 2008 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> Link to comment https://forums.phpfreaks.com/topic/85630-solved-counting-variables/#findComment-440192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.