Droll Numbers In C++

Introduction

Numbers possess fascinating properties, which makes them an exciting topic in both mathematics and programming. One such intriguing category is Droll Numbers . In this article, we will explore what Droll Numbers are, define their properties, and implement an efficient C++ program to identify them.

Problem Statement:

A Droll Number is a number N such that the sum of its even prime divisors is equal to the sum of its odd prime divisors.

Key Considerations:

Several key considerations of Droll Numbers in C++ are as follows:

  • Prime Factors: A number’s prime divisors are the prime numbers that divide it completely.
  • Even Prime Divisors: The only even prime number is 2 .
  • Odd Prime Divisors: These include 3, 5, 7, 11, ... etc.
  • Condition for a Droll Number :

The sum of all even prime divisors must be equal to the sum of all odd prime divisors.

Examples:

Example 1:

N = 30

  • Prime factors: 2, 3, 5
  • Sum of even prime divisors: 2
  • Sum of odd prime divisors: 3 + 5 = 8
  • Since 2 ≠ 8, 30 is NOT a Droll Number

Example 2:

N = 42

  • Prime factors: 2, 3, 7
  • Sum of even prime divisors: 2
  • Sum of odd prime divisors: 3 + 7 = 10
  • Since 2 ≠ 10, 42 is NOT a Droll Number

Example 3:

N = 6

  • Prime factors: 2, 3
  • Sum of even prime divisors: 2
  • Sum of odd prime divisors: 3
  • Since 2 = 3, 6 is NOT a Droll Number

Example 4:

N = 72

  • Prime factors: 2, 2, 2, 3, 3
  • Sum of even prime divisors: 2+2+2 = 6
  • Sum of odd prime divisors: 3+3 = 6
  • Since 6 = 6, 72 is a Droll Number
  • Approach to Solve the Problem:

    1. Find the Prime Factors of N

  • Use trial division to extract prime divisors.
  • 2. Categorize Even and Odd Prime Factors

  • If a prime factor is 2, add it to the sum of even prime divisors.
  • If a prime factor is odd, add it to the sum of odd prime divisors.
  • 3. Check if the Sums are Equal

  • If sum of even primes == sum of odd primes, N is a Droll Number.
  • Program 1:

Let us take an example to illustrate the Droll Numbers in C++.

Example

#include <iostream>

#include <cmath>

using namespace std;

// Function to check if a number is a Droll Number

bool checkDroll(int num) {

    if (num == 1)

        return false;

    // To store sum of even prime factors

    int sumEvenPrimes = 0;

    // To store sum of odd prime factors

    int sumOddPrimes = 0;

    // Count occurrences of 2 in factorization

    while (num % 2 == 0) {

        sumEvenPrimes += 2;

        num /= 2;

    }

    // Checking for odd prime factors

    for (int factor = 3; factor <= sqrt(num); factor += 2) {

        while (num % factor == 0) {

            sumOddPrimes += factor;

            num /= factor;

        }

    }

    // If num is a prime greater than 2, add to sumOddPrimes

    if (num > 2)

        sumOddPrimes += num;

    // Check if sums are equal

    return sumEvenPrimes == sumOddPrimes;

}

// Main function

int main() {

    cout<<"Enter a number:";

    int number;  // Given Number

    cin>>number;

    // Check and output result

    if (checkDroll(number))

        cout << "Yes, it is a Droll Number." << endl;

    else

        cout << "No, it is NOT a Droll Number." << endl;

    return 0;

}

Output:

Output

Enter a number:6272

Yes, it is a Droll Number.

Explanation:

  • Identify All Prime Factors Find the prime numbers that divide the given number N. Example: If N = 72, its prime factors are 2 and 3.
  • Separate Even and Odd Prime Factors Even prime factors: Only 2 (since 2 is the only even prime). Odd prime factors: All other prime numbers (3, 5, 7, etc.).
  • Calculate the Sums Add up all occurrences of even prime factors. Add up all occurrences of odd prime factors.
  • Compare the Sums If sum of even prime factors = sum of odd prime factors, the number is a Droll Number. Otherwise, it is not a Droll Number.
  • Find the prime numbers that divide the given number N.
  • Example: If N = 72, its prime factors are 2 and 3.
  • Even prime factors: Only 2 (since 2 is the only even prime).
  • Odd prime factors: All other prime numbers (3, 5, 7, etc.).
  • Add up all occurrences of even prime factors.
  • Add up all occurrences of odd prime factors.
  • If sum of even prime factors = sum of odd prime factors, the number is a Droll Number.
  • Otherwise, it is not a Droll Number.
  • Program 2:

Let us take another example to illustrate the Droll Numbers in C++.

Example

#include <iostream>

#include <vector>

#include <cmath>

using namespace std;

// Function to get the prime factors of a number

void getPrimeFactors(int num, vector<int>& factors) {

    // Check for factor 2

    while (num % 2 == 0) {

        factors.push_back(2);

        num /= 2;

    }

    // Check for odd prime factors

    for (int i = 3; i <= sqrt(num); i += 2) {

        while (num % i == 0) {

            factors.push_back(i);

            num /= i;

        }

    }

    // If num is a prime number greater than 2

    if (num > 2) {

        factors.push_back(num);

    }

}

// Function to calculate the sum of even and odd prime factors separately

void calculateSums(const vector<int>& factors, int& sumEven, int& sumOdd) {

    sumEven = 0;

    sumOdd = 0;

    for (int factor : factors) {

        if (factor % 2 == 0)

            sumEven += factor;

        else

            sumOdd += factor;

    }

}

// Function to check if a number is a Droll Number

bool isDrollNumber(int num) {

    if (num <= 1) 

        return false;

    vector<int> factors;

    getPrimeFactors(num, factors);

    int sumEven = 0, sumOdd = 0;

    calculateSums(factors, sumEven, sumOdd);

    return sumEven == sumOdd;

}

// Main function to test multiple numbers

int main() {

    int testCases;

    

    cout << "Enter the number of test cases: ";

    cin >> testCases;

    while (testCases--) {

        int num;

        cout << "Enter a number: ";

        cin >> num;

        if (isDrollNumber(num))

            cout << num << " is a Droll Number \n";

        else

            cout << num << " is NOT a Droll Number \n";

    }

    return 0;

}

Output:

Output

Enter the number of test cases: 10

Enter a number: 88

88 is NOT a Droll Number

Enter a number: 99

99 is NOT a Droll Number

Enter a number: 1

1 is NOT a Droll Number

Enter a number: 22

22 is NOT a Droll Number

Enter a number: 72

72 is a Droll Number

Enter a number: 6727

6727 is NOT a Droll Number

Enter a number: 6289

6289 is NOT a Droll Number

Enter a number: 66

66 is NOT a Droll Number

Enter a number: 11111

11111 is NOT a Droll Number

Enter a number: 6272

6272 is a Droll Number

Explanation:

  • Prime Factorization (getPrimeFactors) It extracts the prime factors of a number. First, it repeatedly divides by 2 (even prime). After that, it checks for odd prime factors up to sqrt(num). If the remaining num is greater than 2, it is also a prime factor.
  • Summing Even and Odd Prime Factors (calculateSums) It iterates through the list of prime factors. Separately sums up the even and odd factors.
  • Droll Number Check (isDrollNumber) It computes the prime factors and their sums. It returns true if the sum of even and odd prime factors is equal.
  • User Input and Execution (main) It accepts multiple test cases from the user. It reads a number and checks if it's a Droll Number.
  • It extracts the prime factors of a number.
  • First, it repeatedly divides by 2 (even prime).
  • After that, it checks for odd prime factors up to sqrt(num).
  • If the remaining num is greater than 2, it is also a prime factor.
  • It iterates through the list of prime factors.
  • Separately sums up the even and odd factors.
  • It computes the prime factors and their sums.
  • It returns true if the sum of even and odd prime factors is equal.
  • It accepts multiple test cases from the user.
  • It reads a number and checks if it's a Droll Number.
  • Program 3:

Let us take another example to illustrate the Droll Numbers in C++.

Example

#include <iostream>

#include <vector>

#include <cmath>

#include <unordered_map>

#include <random>

#include<bits/stdc++.h>

using namespace std;

// Function to generate primes using the Sieve of Eratosthenes

const int MAX_N = 1000000; // Precompute primes up to 1 million

vector<bool> isPrime(MAX_N + 1, true);

vector<int> primes;

void sieve() {

    isPrime[0] = isPrime[1] = false;

    for (int i = 2; i * i <= MAX_N; i++) {

        if (isPrime[i]) {

            for (int j = i * i; j <= MAX_N; j += i) {

                isPrime[j] = false;

            }

        }

    }

    for (int i = 2; i <= MAX_N; i++) {

        if (isPrime[i]) primes.push_back(i);

    }

}

// Pollard's Rho Algorithm for Fast Factorization

long long modularMultiply(long long a, long long b, long long mod) {

    return (__int128)a * b % mod; // Handles overflow issues

}

long long modularExponentiate(long long base, long long exp, long long mod) {

    long long result = 1;

    while (exp > 0) {

        if (exp % 2 == 1) result = modularMultiply(result, base, mod);

        base = modularMultiply(base, base, mod);

        exp /= 2;

    }

    return result;

}

// Randomized Pollard's Rho for faster prime factorization

long long pollardsRho(long long n) {

    if (n % 2 == 0) return 2;

    random_device rd;

    mt19937 rng(rd());

    uniform_int_distribution<long long> dist(2, n - 1);

    

    long long x = dist(rng), y = x, c = dist(rng);

    long long d = 1;

    

    auto f = [&](long long v) { return (modularMultiply(v, v, n) + c) % n; };

    

    while (d == 1) {

        x = f(x);

        y = f(f(y));

        d = __gcd(abs(x - y), n);

    }

    return d;

}

// Function to find prime factors using Pollard’s Rho

void getPrimeFactors(long long num, unordered_map<int, int>& factors) {

    while (num % 2 == 0) {

        factors[2]++;

        num /= 2;

    }

    

    while (num > 1) {

        if (isPrime[num]) {

            factors[num]++;

            break;

        }

        

        long long factor = pollardsRho(num);

        while (num % factor == 0) {

            factors[factor]++;

            num /= factor;

        }

    }

}

// Function to check if a number is a Droll Number

bool isDrollNumber(long long num) {

    if (num <= 1) return false;

    

    unordered_map<int, int> factors;

    getPrimeFactors(num, factors);

    

    int sumEven = 0, sumOdd = 0;

    for (auto& pair : factors) {

        int prime = pair.first;

        int sumFactor = prime * pair.second; // Sum of prime factor occurrences

        

        if (prime % 2 == 0) sumEven += sumFactor;

        else sumOdd += sumFactor;

    }

    

    return sumEven == sumOdd;

}

// Driver Code

int main() {

    sieve(); // Precompute primes

    

    long long N;

    cout << "Enter a number: ";

    cin >> N;

    

    if (isDrollNumber(N))

        cout << N << " is a Droll Number.\n";

    else

        cout << N << " is NOT a Droll Number.\n";

    

    return 0;

}

Output:

Output

Enter a number: 72

72 is a Droll Number.

Explanation:

  • Uses Pollard’s Rho Algorithm (Faster Factorization) Unlike basic trial division (O(√N)), Pollard’s Rho factorizes in nearly O(N^1/4). It efficiently handles large numbers (up to 10^18), making it ideal for big number computations.
  • Precomputes Primes Using the Sieve of Eratosthenes Instead of checking prime numbers repeatedly, we precompute primes up to 1 million (O(N log log N)). It reduces repeated prime checks and speeds up execution.
  • Efficient Prime Factorization with Hash Map Instead of storing prime factors in a vector, we use an unordered_map to count prime occurrences. It prevents duplicate calculations, which reduces time complexity to O(log N) per factor.
  • Early Exit Conditions for Faster Execution If a number becomes prime during factorization, we immediately return, avoiding unnecessary checks. If the number is 1 or non-factorizable, we return early to save computation.
  • Handles Large Numbers Efficiently It uses __int128 in modular multiplication to prevent overflow issues when working with big numbers. It allows the program to handle numbers up to 10^18 without integer overflow.
  • Unlike basic trial division (O(√N)), Pollard’s Rho factorizes in nearly O(N^1/4).
  • It efficiently handles large numbers (up to 10^18), making it ideal for big number computations.
  • Instead of checking prime numbers repeatedly, we precompute primes up to 1 million (O(N log log N)).
  • It reduces repeated prime checks and speeds up execution.
  • Instead of storing prime factors in a vector, we use an unordered_map to count prime occurrences.
  • It prevents duplicate calculations, which reduces time complexity to O(log N) per factor.
  • If a number becomes prime during factorization, we immediately return, avoiding unnecessary checks.
  • If the number is 1 or non-factorizable, we return early to save computation.
  • It uses __int128 in modular multiplication to prevent overflow issues when working with big numbers.
  • It allows the program to handle numbers up to 10^18 without integer overflow.
  • Advantages of Studying Droll Numbers:

Several advantages of Droll Numbers in C++ are as follows:

  • Enhances Problem-Solving Skills Understanding Droll Numbers involves prime factorization, loops, condition checking, and mathematical logic. It helps develop strong analytical thinking and problem-solving skills, which are essential in programming contests.
  • Strengthens Number Theory Knowledge The concept of Droll Numbers is deeply connected to prime numbers and divisibility rules.
  • Useful in Cryptography and Security Many encryption algorithms rely on prime numbers and factorization (e.g., RSA encryption). Understanding Droll Numbers helps in mastering modular arithmetic and mathematical security concepts.
  • Helps in Competitive Programming Problems based on number properties frequently appear in coding competitions (Codeforces, LeetCode, CodeChef, etc.). Droll Number problems involve efficient factorization and sum calculation, which are key skills for competitive programmers.
  • Efficient Algorithm Design Droll Number algorithms improve efficiency in solving mathematical problems, which leads to better optimization techniques in prime number-related algorithms. It helps in designing better mathematical and computational models.
  • Understanding Droll Numbers involves prime factorization, loops, condition checking, and mathematical logic.
  • It helps develop strong analytical thinking and problem-solving skills, which are essential in programming contests.
  • The concept of Droll Numbers is deeply connected to prime numbers and divisibility rules.
  • Many encryption algorithms rely on prime numbers and factorization (e.g., RSA encryption).
  • Understanding Droll Numbers helps in mastering modular arithmetic and mathematical security concepts.
  • Problems based on number properties frequently appear in coding competitions (Codeforces, LeetCode, CodeChef, etc.).
  • Droll Number problems involve efficient factorization and sum calculation, which are key skills for competitive programmers.
  • Droll Number algorithms improve efficiency in solving mathematical problems, which leads to better optimization techniques in prime number-related algorithms.
  • It helps in designing better mathematical and computational models.
  • Applications of Droll Numbers:

Several applications of Droll Numbers in C++ are as follows:

  • Mathematics and Number Theory Research Droll Numbers provide interesting insights into mathematical properties of numbers. They can be used to discover new number sequences and properties of prime numbers.
  • computer science and Algorithm Development Algorithms based on factorization are widely used in data compression, hashing, and indexing. Understanding Droll Numbers helps in optimizing factorization algorithms.
  • Cryptography and Cybersecurity Cryptographic techniques like RSA encryption depend on prime factorization. Studying Droll Numbers strengthens the knowledge of modular arithmetic and secure algorithms.
  • Digital Forensics and Data Integrity Many forensic tools use mathematical algorithms to verify data authenticity. Droll Number properties can help in detecting anomalies in large datasets.
  • Coding Interviews and Competitive Programming Questions based on prime numbers and mathematical sums are common in FAANG (Facebook, Amazon, Apple, Netflix, Google) interviews. Mastering Droll Numbers helps in tackling number theory-based questions easily.
  • Droll Numbers provide interesting insights into mathematical properties of numbers.
  • They can be used to discover new number sequences and properties of prime numbers.
  • Algorithms based on factorization are widely used in data compression, hashing, and indexing.
  • Understanding Droll Numbers helps in optimizing factorization algorithms.
  • Cryptographic techniques like RSA encryption depend on prime factorization.
  • Studying Droll Numbers strengthens the knowledge of modular arithmetic and secure algorithms.
  • Many forensic tools use mathematical algorithms to verify data authenticity.
  • Droll Number properties can help in detecting anomalies in large datasets.
  • Questions based on prime numbers and mathematical sums are common in FAANG (Facebook, Amazon, Apple, Netflix, Google) interviews.
  • Mastering Droll Numbers helps in tackling number theory-based questions easily.
  • Conclusion:

In conclusion, Droll Numbers introduce a unique way to categorize numbers based on their even and odd prime divisors. This problem is an excellent way to practice number theory, prime factorization, and logical thinking in C++.

Input Required

This code uses input(). Please provide values below: