Palindromic divisors of a number Last Updated : 26 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Prerequisite: Find all divisors of a natural number Given a number N. The task is to find all the palindromic divisors of N. Examples: Input: N = 66 Output: 1 2 3 6 11 22 33 66 Input: N = 808 Output: 1 2 4 8 101 202 404 808 Approach: Find all the divisors of N using approach discussed in this article.For each divisors D, check whether D is palindromic or not.Repeat the above step for all the divisors. Below is the implementation of the above approach: C++14 // C++ program to find all the palindromic // divisors of a number #include "bits/stdc++.h" using namespace std; // Function to check is num is palindromic // or not bool isPalindrome(int n) { // Convert n to string str string str = to_string(n); // Starting and ending index of // string str int s = 0, e = str.length() - 1; while (s < e) { // If char at s and e are // not equals then return // false if (str[s] != str[e]) { return false; } s++; e--; } return true; } // Function to find palindromic divisors void palindromicDivisors(int n) { // To store the palindromic divisors of // number n vector<int> PalindromDivisors; for (int i = 1; i <= sqrt(n); i++) { // If n is divisible by i if (n % i == 0) { // Check if number is a perfect square if (n / i == i) { // Check divisor is palindromic, // then store it if (isPalindrome(i)) { PalindromDivisors.push_back(i); } } else { // Check if divisors are palindrome if (isPalindrome(i)) { PalindromDivisors.push_back(i); } // Check if n / divisors is palindromic // or not if (isPalindrome(n / i)) { PalindromDivisors.push_back(n / i); } } } } // Print all palindromic divisors in sorted order sort(PalindromDivisors.begin(), PalindromDivisors.end()); for (int i = 0; i < PalindromDivisors.size(); i++) { cout << PalindromDivisors[i] << " "; } } // Driver code int main() { int n = 66; // Function call to find all palindromic // divisors palindromicDivisors(n); } Java // Java program to find all the palindromic // divisors of a number import java.util.*; class GFG { // Function to check is num is palindromic // or not static boolean isPalindrome(int n) { // Convert n to String str String str = String.valueOf(n); // Starting and ending index of // String str int s = 0, e = str.length() - 1; while (s < e) { // If char at s and e are // not equals then return // false if (str.charAt(s) != str.charAt(e)) { return false; } s++; e--; } return true; } // Function to find palindromic divisors static void palindromicDivisors(int n) { // To store the palindromic divisors of // number n Vector<Integer> PalindromDivisors = new Vector<Integer>(); for (int i = 1; i <= Math.sqrt(n); i++) { // If n is divisible by i if (n % i == 0) { // Check if number is a perfect square if (n / i == i) { // Check divisor is palindromic, // then store it if (isPalindrome(i)) { PalindromDivisors.add(i); } } else { // Check if divisors are palindrome if (isPalindrome(i)) { PalindromDivisors.add(i); } // Check if n / divisors is palindromic // or not if (isPalindrome(n / i)) { PalindromDivisors.add(n / i); } } } } // Print all palindromic divisors in sorted order Collections.sort(PalindromDivisors); for (int i = 0; i < PalindromDivisors.size(); i++) { System.out.print(PalindromDivisors.get(i)+ " "); } } // Driver code public static void main(String[] args) { int n = 66; // Function call to find all palindromic // divisors palindromicDivisors(n); } } // This code is contributed by 29AjayKumar Python3 # Python3 program to find all the palindromic # divisors of a number from math import sqrt; # Function to check is num is palindromic # or not def isPalindrome(n) : # Convert n to string str string = str(n); # Starting and ending index of # string str s = 0; e = len(string) - 1; while (s < e) : # If char at s and e are # not equals then return # false if (string[s] != string[e]) : return False; s += 1; e -= 1; return True; # Function to find palindromic divisors def palindromicDivisors(n) : # To store the palindromic divisors of # number n PalindromDivisors = []; for i in range(1, int(sqrt(n))) : # If n is divisible by i if (n % i == 0) : # Check if number is a perfect square if (n // i == i) : # Check divisor is palindromic, # then store it if (isPalindrome(i)) : PalindromDivisors.append(i); else : # Check if divisors are palindrome if (isPalindrome(i)) : PalindromDivisors.append(i); # Check if n / divisors is palindromic # or not if (isPalindrome(n // i)) : PalindromDivisors.append(n // i); # Print all palindromic divisors in sorted order PalindromDivisors.sort(); for i in range(len( PalindromDivisors)) : print(PalindromDivisors[i] ,end=" "); # Driver code if __name__ == "__main__" : n = 66; # Function call to find all palindromic # divisors palindromicDivisors(n); # This code is contributed by AnkitRai01 C# // C# program to find all the palindromic // divisors of a number using System; using System.Collections.Generic; class GFG { // Function to check is num is palindromic // or not static bool isPalindrome(int n) { // Convert n to String str String str = String.Join("",n); // Starting and ending index of // String str int s = 0, e = str.Length - 1; while (s < e) { // If char at s and e are // not equals then return // false if (str[s] != str[e]) { return false; } s++; e--; } return true; } // Function to find palindromic divisors static void palindromicDivisors(int n) { // To store the palindromic divisors of // number n List<int> PalindromDivisors = new List<int>(); for (int i = 1; i <= Math.Sqrt(n); i++) { // If n is divisible by i if (n % i == 0) { // Check if number is a perfect square if (n / i == i) { // Check divisor is palindromic, // then store it if (isPalindrome(i)) { PalindromDivisors.Add(i); } } else { // Check if divisors are palindrome if (isPalindrome(i)) { PalindromDivisors.Add(i); } // Check if n / divisors is palindromic // or not if (isPalindrome(n / i)) { PalindromDivisors.Add(n / i); } } } } // Print all palindromic divisors in sorted order PalindromDivisors.Sort(); for (int i = 0; i < PalindromDivisors.Count; i++) { Console.Write(PalindromDivisors[i]+ " "); } } // Driver code public static void Main(String[] args) { int n = 66; // Function call to find all palindromic // divisors palindromicDivisors(n); } } // This code is contributed by PrinciRaj1992 JavaScript <script> // Javascript program to find all the palindromic // divisors of a number // Function to check is num is palindromic // or not function isPalindrome(n) { // Convert n to string str var str = (n.toString()); // Starting and ending index of // string str var s = 0, e = str.length - 1; while (s < e) { // If char at s and e are // not equals then return // false if (str[s] != str[e]) { return false; } s++; e--; } return true; } // Function to find palindromic divisors function palindromicDivisors(n) { // To store the palindromic divisors of // number n var PalindromDivisors = []; for(var i = 1; i <= parseInt(Math.sqrt(n)); i++) { // If n is divisible by i if (n % i == 0) { // Check if number is a perfect square if (n / i == i) { // Check divisor is palindromic, // then store it if (isPalindrome(i)) { PalindromDivisors.push(i); } } else { // Check if divisors are palindrome if (isPalindrome(i)) { PalindromDivisors.push(i); } // Check if n / divisors is palindromic // or not if (isPalindrome(n / i)) { PalindromDivisors.push(n / i); } } } } // Print all palindromic divisors in sorted order PalindromDivisors.sort((a, b) => a - b) for(var i = 0; i < PalindromDivisors.length; i++) { document.write( PalindromDivisors[i] + " "); } } // Driver code var n = 66; // Function call to find all palindromic // divisors palindromicDivisors(n); // This code is contributed by rrrtnx </script> Output: 1 2 3 6 11 22 33 66 Time Complexity: O(N*log N) Auxiliary Space: O(sqrt(n)) Comment More infoAdvertise with us Next Article Palindromic divisors of a number A AmanGupta65 Follow Improve Article Tags : Mathematical DSA Arrays palindrome divisors factor +2 More Practice Tags : ArraysMathematicalpalindrome Similar Reads Queries to print all divisors of number Given a positive integer 'n' and query 'q'. Print all the divisors of number 'n'. Input: 6 Output: 1 2 3 6 Explanation Divisors of 6 are: 1, 2, 3, 6 Input: 10 Output: 1 2 5 10 Naive approach is to iterate through 1 to sqrt(n) for every query 'q' and print the divisors accordingly. See this to unders 5 min read Check if a Number is Palindromic Prime A palindromic prime (sometimes called a palprime) is a prime number that is also a palindromic number. Given a number n, print all palindromic primes smaller than or equal to n. For example, If n is 10, the output should be â2, 3, 5, 7'. And if n is 20, the output should be â2, 3, 5, 7, 11'.Idea is 15 min read Largest palindromic number in an array Given an array of non-negative integers arr[]. The task is to find the largest number in the array which is palindrome. If no such number exits then print -1. Examples: Input: arr[] = {1, 232, 54545, 999991}; Output: 54545 Input: arr[] = {1, 2, 3, 4, 5, 50}; Output: 5 Recommended: Please try your ap 15+ min read Find all palindrome numbers of given digits Given an integer D, the task is to find all the D-digit palindrome numbers.Examples: Input: D = 1 Output: 1 2 3 4 5 6 7 8 9Input: D = 2 Output: 11 22 33 44 55 66 77 88 99 Approach: Numbers with D-digits start from 10(D - 1) to 10D - 1. So, start checking every number from this interval whether it is 5 min read Common Divisors of Two Numbers Given two integer numbers, the task is to find count of all common divisors of given numbers? Examples : Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common div 15+ min read Count of N digit palindromic numbers divisible by 9 Given an integer N, the task is to count the number of N digit palindromic numbers containing digits from 1 to 9 and divisible by 9. Examples: Input: N = 1 Output: 1 Explanation: Only 9 is 1 digit number which is palindrome and divisible by 9.Input: N = 3 Output: 9 Explanation: Three digit numbers t 4 min read Sum of all proper divisors of a natural number Given a natural number, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number. For example, number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.Examples : Input : num = 10 6 min read N'th palindrome of K digits Given two integers n and k, Find the lexicographical nth palindrome of k digits.Examples: Input : n = 5, k = 4 Output : 1441 Explanation: 4 digit lexicographical palindromes are: 1001, 1111, 1221, 1331, 1441 5th palindrome = 1441 Input : n = 4, k = 6 Output : 103301 Naive Approach A brute force is t 13 min read Check if a number with even number of digits is palindrome or not Given a number N containing an even number of digits. The task is to check whether that number is palindrome or not. Examples: Input: N = 123321 Output: Palindrome Input: 1234 Output: Not palindrome A Naive Approach is to traverse from the front and back of that number and stop where they do not mat 4 min read Check if number is palindrome or not in Octal Given a number which may be in octal or in decimal. If the number is not octal then convert it into octal then check if it is palindrome or not. If it is palindrome then print 1 otherwise print 0. If the number is already in octal then check if the number is palindrome or not.Examples : Input :n = 1 7 min read Like