Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PHP Challenges: 3n + 1 problem

PHP Challenges - 1: Exercise-18 with Solution

Write a PHP program where you take any positive integer n, if n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process until you reach 1.

Input : 12

According to Wikipedia the Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture.
The conjecture can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called "Half Or Triple Plus One") indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
Example :
For instance, starting with n = 12, one gets the sequence 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
n = 19, for example, takes longer to reach 1: 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.

Sample Solution :

PHP Code :

<?php
function collatz_sequence($x)
{
    $num_seq = [$x];
    if ($x < 1)
    {
       return [];
    }
    while ($x > 1)
      {
       if ($x % 2 == 0)
       {
         $x = $x / 2;
       }
       else
       {
         $x = 3 * $x + 1;
       }
    # Added line
     array_push($num_seq, $x);
      }
    return $num_seq;
}
print_r(collatz_sequence(12));
print_r(collatz_sequence(19));
?>

Sample Output:

Array                                                       
(                                                           
    [0] => 12                                               
    [1] => 6                                                
    [2] => 3                                                
    [3] => 10                                               
    [4] => 5                                                
    [5] => 16                                               
    [6] => 8                                                
    [7] => 4                                                
    [8] => 2                                                
    [9] => 1                                                
)                                                           
Array                                                       
(                                                           
    [0] => 19                                               
    [1] => 58                                               
    [2] => 29   
	[3] => 88                                               
    [4] => 44                                               
    [5] => 22                                               
    [6] => 11                                               
    [7] => 34                                               
    [8] => 17                                               
    [9] => 52                                               
    [10] => 26                                              
    [11] => 13                                              
    [12] => 40                                              
    [13] => 20                                              
    [14] => 10                                              
    [15] => 5                                               
    [16] => 16                                              
    [17] => 8                                               
    [18] => 4                                               
    [19] => 2                                               
    [20] => 1                                               
)

Flowchart:

PHP Flowchart: 3n + 1 problem

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to compute the sum of the two reversed numbers and display the sum in reversed form.
Next: Write a PHP program to check whether a given number is an ugly number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



PHP: Tips of the Day

How to Sort Multi-dimensional Array by Value?

Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

Example:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
    return $a['order'] - $b['order'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
    $retval = $a['order'] <=> $b['order'];
    if ($retval == 0) {
        $retval = $a['suborder'] <=> $b['suborder'];
        if ($retval == 0) {
            $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
        }
    }
    return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual

Ref : https://bit.ly/3i77vCC