Finding Numerical Palindromes in PHP

In this tutorial, we’ll explore a PHP script that finds numerical palindromes, starting from a specified number. A numerical palindrome is a number that reads the same forward and backward, such as 121 or 1331.

 

This guide will walk you through the code step-by-step, explaining each part and showing how we can find a specified number of palindromes starting from a given point.

What This Code Does

Our script contains two main functions:

  1. findNumericalPalindromes($count, $startNumber) – Finds the first $count numerical palindromes starting from $startNumber.
  2. reverseNumber($number) – Reverses a number to help check if it is a palindrome.

Full Code Implementation

Let’s dive into the code, with explanations following each part.

				
					<?php
function findNumericalPalindromes($count, $startNumber) {
  $palindromes = [];  // Array to store the found palindromes
  
  while (count($palindromes) < $count) {
    $reversedNumber = reverseNumber($startNumber);
    
    // Check if the number is equal to its reverse and is greater than 9
    if ($reversedNumber == $startNumber && $startNumber > 9) {
      $palindromes[] = $startNumber;  // Add to result if it’s a palindrome
    }
    $startNumber++;  // Move to the next number for checking
  }
  
  return $palindromes;
}

// Helper function to reverse the digits of a given number
function reverseNumber($number) {
  $reversed = 0;
  
  while ($number > 0) {
    $reversed = ($reversed * 10) + ($number % 10);  // Add the last digit of $number to $reversed
    $number = (int)($number / 10);  // Remove the last digit from $number
  }
  
  return $reversed;
}

// Test Cases
print_r(findNumericalPalindromes(3, 4));    // Output first 3 palindromes starting from 4
print_r(findNumericalPalindromes(1, 77));   // Output first palindrome starting from 77
print_r(findNumericalPalindromes(4, 141));  // Output first 4 palindromes starting from 141
?>

				
			

Code Explanation

Let’s break down the code in detail.

1. findNumericalPalindromes Function

This is the main function that finds palindromes. It takes two parameters:

  • $count: The number of palindromes we want to find.
  • $startNumber: The starting number from which to begin checking for palindromes.

How It Works:

  • Initialize $palindromes: An empty array to hold the palindromes found.
  • While Loop: Runs until the length of $palindromes reaches $count.
    • For each number, it calls reverseNumber($startNumber) to get the reversed version of the number.
    • It checks if the original number ($startNumber) is equal to its reversed version ($reversedNumber). This confirms whether the number is a palindrome.
    • If the number is a palindrome and is greater than 9, it is added to $palindromes.
    • Finally, the function increments $startNumber to check the next number.
  • Return Statement: After finding the required number of palindromes, it returns the array $palindromes.

2. reverseNumber Function

This helper function takes a single number as input and returns its reverse.

How It Works:

  • Initialize $reversed: Starts at 0 and will accumulate the digits of $number in reverse order.
  • While Loop: Repeats as long as $number is greater than 0.
    • ($number % 10): Gets the last digit of $number.
    • ($reversed * 10) + ($number % 10): Appends this last digit to $reversed.
    • (int)($number / 10): Removes the last digit from $number.
  • Return Statement: Once the loop completes, $reversed contains the reverse of the original number, which is returned.

Test Cases

At the end of the script, we have three test cases to see our code in action:

				
					print_r(findNumericalPalindromes(3, 4));    // Output first 3 palindromes starting from 4
print_r(findNumericalPalindromes(1, 77));   // Output first palindrome starting from 77
print_r(findNumericalPalindromes(4, 141));  // Output first 4 palindromes starting from 141

				
			

Each print_r statement displays an array with the palindromes found, which lets us verify the code works as expected.

Sample Output

When running this code, here’s the output you should expect:

				
					Array
(
    [0] => 11
    [1] => 22
    [2] => 33
)
Array
(
    [0] => 77
)
Array
(
    [0] => 141
    [1] => 151
    [2] => 161
    [3] => 171
)

				
			

Summary

This PHP script provides an efficient way to find numerical palindromes starting from a specified number. By using two functions, we’ve structured the code to keep it clean and modular:

  • findNumericalPalindromes handles the main logic of finding the palindromes.
  • reverseNumber serves as a helper function to reverse a number, which is crucial for checking if a number is a palindrome.

This code demonstrates how basic logic and loops can solve interesting problems with numbers, and it can be adapted to other use cases where palindromic sequences or similar patterns are needed.

Leave a Reply

Your email address will not be published. Required fields are marked *