w3resource

PHP Challenges: Get the Hamming numbers upto a given numbers also check whether a given number is a Hamming number

PHP Challenges - 1: Exercise-20 with Solution

Write a PHP program to get the Hamming numbers upto a given numbers also check whether a given number is a Hamming number.

Input : 1

Hamming numbers are numbers of the form
H = 2i × 3j × 5k
Where i, j, k ≥ 0
The sequence of Hamming numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27 ... consists of all numbers of the form 2i•3j•5k where i, j and k are non-negative integers.

Explanation :

PHP: Hamming numbers upto a given numbers

Sample Solution :

PHP Code :

<?php
// Function to check if a number is a Hamming number
function is_hamming_numbers($x)
{
    // Check if the number is 1, which is always a Hamming number
    if ($x == 1)
    {
        return "Hamming Number";
    }
    // Check if the number is divisible by 2
    if ($x % 2 == 0)
    {
        // If so, recursively call the function with the number divided by 2
        return is_hamming_numbers($x / 2);
    }
    // Check if the number is divisible by 3
    if ($x % 3 == 0)
    {
        // If so, recursively call the function with the number divided by 3
        return is_hamming_numbers($x / 3);
    }
    // Check if the number is divisible by 5
    if ($x % 5 == 0)
    {
        // If so, recursively call the function with the number divided by 5
        return is_hamming_numbers($x / 5);
    }
    // If none of the above conditions are met, the number is not a Hamming number
    return "Not a Hamming Number";
}

// Function to generate the Hamming numbers sequence up to a given number
function hamming_numbers_sequence($x)
{
    // Check if the number is 1, which is always a Hamming number
    if ($x == 1)
    {
        return "Hamming Number";
    }
    // Recursively call the function with the previous number
    hamming_numbers_sequence($x - 1);
    // Check if the current number is a Hamming number
    if (is_hamming_numbers($x) == "Hamming Number")
    {
        // If so, print the number followed by a comma
        echo($x) . ",";
    }
}

// Test the is_hamming_numbers function with different inputs and print the results
print_r(is_hamming_numbers(7) . "\n");
print_r(is_hamming_numbers(1) . "\n");

// Generate the Hamming numbers sequence up to 24
hamming_numbers_sequence(24);
?>

Explanation:

Here is a brief explanation of the above PHP code:

  • Function definitions:
    • The code defines two functions:
      • is_hamming_numbers($x): This function checks if a given number is a Hamming number. A Hamming number is a positive integer that has only 2, 3, or 5 as its prime factors.
      • hamming_numbers_sequence($x): This function generates the Hamming numbers sequence up to a given number.
  • is_hamming_numbers Function:
    • This function recursively checks if the given number is divisible by 2, 3, or 5. If it is, it divides the number by the divisor and recursively calls itself with the result until the number becomes 1. If none of these conditions are met, it returns "Not a Hamming Number".
  • hamming_numbers_sequence Function:
    • This function recursively generates the Hamming numbers sequence by calling itself with the previous number until it reaches 1. For each number, it checks if it's a Hamming number using the "is_hamming_numbers()" function. If it is, it prints the number followed by a comma.
  • Function Call & Testing:
    • The "is_hamming_numbers()" function is tested with different inputs (7 and 1), and the results are printed.
    • The "hamming_numbers_sequence()" function is called to generate the Hamming numbers sequence up to 24, and the sequence is printed.

Sample Output:

Not a Hamming Number                                        
Hamming Number                                              
2,3,4,5,6,8,9,10,12,15,16,18,20,24 

Flowchart:

PHP Flowchart: Hamming numbers upto a given numbers

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to check whether a given number is an ugly number.
Next: Write a PHP program to check if a given string is an anagram of another given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.