Given an integer **N**, the task is to count the number of ways in which **N** can be expressed as an exponent, i.e., **x ^{y}**, where x and y are positive integers.

**Examples:**

Input:N = 64Output:4Explanation:64 can be expressed as 2^{6}, 4^{3}, 8^{2}and 64^{1}

Input:N = 27Output:2

**Approach:** The idea to solve the given problem is to find the prime factorization of the number **N** and then, find the number of prime factors of the GCD of exponents of the prime factors of the given number **N**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to calculate GCD of a` `// and b using Euclidean Algorithm` `long` `long` `int` `gcd(` `long` `long` `int` `a,` ` ` `long` `long` `int` `b)` `{` ` ` `// Iterate until b is non-zero` ` ` `while` `(b > 0) {` ` ` `long` `long` `int` `rem = a % b;` ` ` `a = b;` ` ` `b = rem;` ` ` `}` ` ` `// Return the GCD` ` ` `return` `a;` `}` `// Function to count the number of` `// ways N can be expressed as x^y` `int` `countNumberOfWays(` `long` `long` `int` `n)` `{` ` ` `// Base Case` ` ` `if` `(n == 1)` ` ` `return` `-1;` ` ` `// Stores the gcd of powers` ` ` `long` `long` `int` `g = 0;` ` ` `int` `power = 0;` ` ` `// Calculate the degree of 2 in N` ` ` `while` `(n % 2 == 0) {` ` ` `power++;` ` ` `n /= 2;` ` ` `}` ` ` `g = gcd(g, power);` ` ` `// Calculate the degree of prime numbers in N` ` ` `for` `(` `int` `i = 3; i <= ` `sqrt` `(n); i += 2) {` ` ` `power = 0;` ` ` `// Calculate the degree of` ` ` `// prime 'i' in N` ` ` `while` `(n % i == 0) {` ` ` `power++;` ` ` `n /= i;` ` ` `}` ` ` `g = gcd(g, power);` ` ` `}` ` ` `// If N is a prime, g becomes 1.` ` ` `if` `(n > 2)` ` ` `g = gcd(g, 1);` ` ` `// Stores the number of ways` ` ` `// to represent N as x^y` ` ` `int` `ways = 1;` ` ` `// Find the number of Factors of g` ` ` `power = 0;` ` ` `while` `(g % 2 == 0) {` ` ` `g /= 2;` ` ` `power++;` ` ` `}` ` ` `// Update the count of ways` ` ` `ways *= (power + 1);` ` ` `// Iterate to find rest of the prime numbers` ` ` `for` `(` `int` `i = 3; i <= ` `sqrt` `(g); i += 2) {` ` ` `power = 0;` ` ` `// Find the power of i` ` ` `while` `(g % i == 0) {` ` ` `power++;` ` ` `g /= i;` ` ` `}` ` ` `// Update the count of ways` ` ` `ways *= (power + 1);` ` ` `}` ` ` `// If g is prime` ` ` `if` `(g > 2)` ` ` `ways *= 2;` ` ` `// Return the total number of ways` ` ` `return` `ways;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 64;` ` ` `cout << countNumberOfWays(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{` `// Function to calculate GCD of a` `// and b using Euclidean Algorithm` `static` `int` `gcd(` `int` `a,` ` ` `int` `b)` `{` ` ` `// Iterate until b is non-zero` ` ` `while` `(b > ` `0` `) {` ` ` `int` `rem = a % b;` ` ` `a = b;` ` ` `b = rem;` ` ` `}` ` ` `// Return the GCD` ` ` `return` `a;` `}` `// Function to count the number of` `// ways N can be expressed as x^y` `static` `int` `countNumberOfWays(` `int` `n)` `{` ` ` ` ` `// Base Case` ` ` `if` `(n == ` `1` `)` ` ` `return` `-` `1` `;` ` ` `// Stores the gcd of powers` ` ` `int` `g = ` `0` `;` ` ` `int` `power = ` `0` `;` ` ` `// Calculate the degree of 2 in N` ` ` `while` `(n % ` `2` `== ` `0` `) {` ` ` `power++;` ` ` `n /= ` `2` `;` ` ` `}` ` ` `g = gcd(g, power);` ` ` `// Calculate the degree of prime numbers in N` ` ` `for` `(` `int` `i = ` `3` `; i <= (` `int` `)Math.sqrt(n); i += ` `2` `) {` ` ` `power = ` `0` `;` ` ` `// Calculate the degree of` ` ` `// prime 'i' in N` ` ` `while` `(n % i == ` `0` `) {` ` ` `power++;` ` ` `n /= i;` ` ` `}` ` ` `g = gcd(g, power);` ` ` `}` ` ` `// If N is a prime, g becomes 1.` ` ` `if` `(n > ` `2` `)` ` ` `g = gcd(g, ` `1` `);` ` ` `// Stores the number of ways` ` ` `// to represent N as x^y` ` ` `int` `ways = ` `1` `;` ` ` `// Find the number of Factors of g` ` ` `power = ` `0` `;` ` ` `while` `(g % ` `2` `== ` `0` `) {` ` ` `g /= ` `2` `;` ` ` `power++;` ` ` `}` ` ` `// Update the count of ways` ` ` `ways *= (power + ` `1` `);` ` ` `// Iterate to find rest of the prime numbers` ` ` `for` `(` `int` `i = ` `3` `; i <= (` `int` `)Math.sqrt(g); i += ` `2` `) {` ` ` `power = ` `0` `;` ` ` `// Find the power of i` ` ` `while` `(g % i == ` `0` `) {` ` ` `power++;` ` ` `g /= i;` ` ` `}` ` ` `// Update the count of ways` ` ` `ways *= (power + ` `1` `);` ` ` `}` ` ` `// If g is prime` ` ` `if` `(g > ` `2` `)` ` ` `ways *= ` `2` `;` ` ` `// Return the total number of ways` ` ` `return` `ways;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `64` `;` ` ` `System.out.print(countNumberOfWays(N));` `}` `}` `// This code is contributed by code_hunt.` |

**Output:**

4

**Time Complexity:** O(√N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.