Panarithmic numbers within a given range

Given two positive numbers A and B.The task is to print all the

Panarithmic Number between two numbers (inclusively).
Panarithmic Numbers or Practical number is a positive integer N such that all positive integers smaller than N can be represented as sums of distinct divisors of N.
For Example:, 12 is a practical number because all the numbers from 1 to 11 can be expressed as sums of its divisors 1, 2, 3, 4, and 6 { 5 = 3 + 2, 7 = 6 + 1, 8 = 6 + 2, 9 = 6 + 3, 10 = 6 + 3 + 1, 11 = 6 + 3 + 2}

Examples:



Input: A = 1 B = 20
Output: 1 2 4 6 8 12 16 18 20
Explanation:
There are 9 Practical Numbers and these are the numbers whose factors can represent all the number smaller than it.
For e.g. 4. Factors of 4 are 1 and 2.
The number 3 can be represented as 1+2 = 3.

Input: A = 100 B = 150
Output: 100 104 108 112 120 126 128 132 140 144 150

Approach:

  1. Iterate from A to B (inclusively).
  2. Check for each number if it is practical number or not.
  3. Compute and store factors of each number within [A, B] one by one and check if all numbers less than the respective numbers can be expressed as the sum of the factors.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print Practical
// Numbers in given range
  
#include <bits/stdc++.h>
using namespace std;
  
// function to compute divisors
// of a number
vector<int> get_divisors(int A)
{
    // vector to store divisors
    vector<int> ans;
  
    // 1 will always be a divisor
    ans.push_back(1);
  
    for (int i = 2; i <= sqrt(A); i++) {
        if (A % i == 0) {
  
            ans.push_back(i);
  
            // check if i is squareroot
            // of A then only one time
            // insert it in ans
            if ((i * i) != A)
                ans.push_back(A / i);
        }
    }
    return ans;
}
  
// function to check that a
// number can be represented as
// sum of distinct divisor or not
bool Sum_Possible(vector<int> set, int sum)
{
    int n = set.size();
  
    // The value of subset[i][j]
    // will be true if
    // there is a subset of
    // set[0..j-1] with sum
    // equal to i
    bool subset[n + 1][sum + 1];
  
    // If sum is 0, then answer is true
    for (int i = 0; i <= n; i++)
        subset[i][0] = true;
  
    // If sum is not 0 and set is empty,
    // then answer is false
    for (int i = 1; i <= sum; i++)
        subset[0][i] = false;
  
    // Fill the subset table
    // in botton up manner
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= sum; j++) {
  
            if (j < set[i - 1])
                subset[i][j] = subset[i - 1][j];
  
            if (j >= set[i - 1])
                subset[i][j]
                    = subset[i - 1][j]
                      || subset[i - 1]
                               [j - set[i - 1]];
        }
    }
  
    // return the possibility
    // of given sum
    return subset[n][sum];
}
  
// function to check a number is
// Practical or not
bool Is_Practical(int A)
{
    // vector to store divisors
    vector<int> divisors;
  
    divisors = get_divisors(A);
    for (int i = 2; i < A; i++) {
  
        if (Sum_Possible(divisors, i) == false)
            return false;
    }
  
    // if all numbers can be
    // represented as sum of
    // unique divisors
    return true;
}
  
// function to print Practical
// Numbers in a range
void print_practica_No(int A, int B)
{
    for (int i = A; i <= B; i++) {
        if (Is_Practical(i) == true) {
            cout << i << " ";
        }
    }
}
  
// Driver Function
int main()
{
    int A = 1, B = 100;
    print_practica_No(A, B);
    return 0;
}

chevron_right


Output:

1 2 4 6 8 12 16 18 20 24 28 30 32 36 40 42 48 54 56 60 64 66 72 78 80 84 88 90 96 100

Time Complexity: O(( B – A) * B 5/2)
Auxiliary Space: O( B 3/2)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.