给定两个正数A和B。任务是打印所有
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}
例子:
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
方法:
- 从A迭代到B(包括B)。
- 检查每个数字是否为实际数字。
- 逐一计算并存储[A,B]中每个数字的因数,并检查是否可以将小于各个数字的所有数字表示为因数之和。
下面是上述方法的实现:
C++
// C++ program to print Practical
// Numbers in given range
#include
using namespace std;
// function to compute divisors
// of a number
vector get_divisors(int A)
{
// vector to store divisors
vector 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 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 bottom 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 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;
}
Java
// Java program to print practical
// Numbers in given range
import java.util.*;
import java.math.*;
class GFG{
// Function to compute divisors
// of a number
static ArrayList get_divisors(int A)
{
// Vector to store divisors
ArrayList ans = new ArrayList<>();
// 1 will always be a divisor
ans.add(1);
for(int i = 2; i <= Math.sqrt(A); i++)
{
if (A % i == 0)
{
ans.add(i);
// Check if i is squareroot
// of A then only one time
// insert it in ans
if ((i * i) != A)
ans.add(A / i);
}
}
return ans;
}
// Function to check that a
// number can be represented as
// sum of distinct divisor or not
static boolean Sum_Possible(ArrayList 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
boolean subset[][] = new boolean[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 bottom up manner
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= sum; j++)
{
if (j < set.get(i - 1))
subset[i][j] = subset[i - 1][j];
if (j >= set.get(i - 1))
subset[i][j] = subset[i - 1][j] ||
subset[i - 1][j -
set.get(i - 1)];
}
}
// Return the possibility
// of given sum
return subset[n][sum];
}
// Function to check a number is
// Practical or not
static boolean Is_Practical(int A)
{
// Vector to store divisors
ArrayList 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
static void print_practica_No(int A, int B)
{
for(int i = A; i <= B; i++)
{
if (Is_Practical(i) == true)
{
System.out.print(i + " ");
}
}
}
// Driver Code
public static void main(String args[])
{
int A = 1, B = 100;
print_practica_No(A, B);
}
}
// This code is contributed by jyoti369
Python3
# Python3 program to print Practical
# Numbers in given range
import math
# Function to compute divisors
# of a number
def get_divisors(A):
# Vector to store divisors
ans = []
# 1 will always be a divisor
ans.append(1)
for i in range(2, math.floor(math.sqrt(A)) + 1):
if (A % i == 0):
ans.append(i)
# Check if i is squareroot
# of A then only one time
# insert it in ans
if ((i * i) != A):
ans.append(A // i)
return ans
# Function to check that a
# number can be represented as
# summ of distinct divisor or not
def summ_Possible(sett, summ):
n = len(sett)
# The value of subsett[i][j] will
# be True if there is a subsett of
# sett[0..j-1] with summ equal to i
subsett = [[0 for i in range(summ + 1)]
for j in range(n + 1)]
# If summ is 0, then answer is True
for i in range(n + 1):
subsett[i][0] = True
# If summ is not 0 and sett is empty,
# then answer is False
for i in range(1, summ + 1):
subsett[0][i] = False
# Fill the subsett table
# in bottom up manner
for i in range(n + 1):
for j in range(summ + 1):
if (j < sett[i - 1]):
subsett[i][j] = subsett[i - 1][j]
if (j >= sett[i - 1]):
subsett[i][j] = (subsett[i - 1][j] or
subsett[i - 1]
[j - sett[i - 1]])
# Return the possibility
# of given summ
return subsett[n][summ]
# Function to check a number
# is Practical or not
def Is_Practical(A):
# Vector to store divisors
divisors = []
divisors = get_divisors(A)
for i in range(2, A):
if (summ_Possible(divisors, i) == False):
return False
# If all numbers can be
# represented as summ of
# unique divisors
return True
# Function to prPractical
# Numbers in a range
def print_practica_No(A, B):
for i in range(A, B + 1):
if (Is_Practical(i) == True):
print(i, end = " ")
# Driver code
A = 1
B = 100
print_practica_No(A, B)
# This code is contributed by shubhamsingh10
C#
// C# program to print practical
// Numbers in given range
using System;
using System.Collections;
class GFG{
// Function to compute divisors
// of a number
static ArrayList get_divisors(int A)
{
// To store divisors
ArrayList ans = new ArrayList();
// 1 will always be a divisor
ans.Add(1);
for(int i = 2;
i <= (int)Math.Sqrt(A);
i++)
{
if (A % i == 0)
{
ans.Add(i);
// Check if i is squareroot
// of A then only one time
// insert it in ans
if ((i * i) != A)
ans.Add(A / i);
}
}
return ans;
}
// Function to check that a
// number can be represented as
// sum of distinct divisor or not
static bool Sum_Possible(ArrayList set,
int sum)
{
int n = set.Count;
// 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 = new bool[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 bottom up manner
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= sum; j++)
{
if (j < (int)set[i - 1])
subset[i, j] = subset[i - 1, j];
if (j >= (int)set[i - 1])
subset[i, j] = subset[i - 1, j] ||
subset[i - 1, j -
(int)set[i - 1]];
}
}
// Return the possibility
// of given sum
return subset[n, sum];
}
// Function to check a number is
// Practical or not
static bool Is_Practical(int A)
{
// To store divisors
ArrayList divisors = new ArrayList();
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
static void print_practica_No(int A, int B)
{
for(int i = A; i <= B; i++)
{
if (Is_Practical(i) == true)
{
Console.Write(i + " ");
}
}
}
// Driver code
public static void Main(string []args)
{
int A = 1, B = 100;
print_practica_No(A, B);
}
}
// This code is contributed by rutvik_56
输出:
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
时间复杂度: O((B – A)* B 5/2 )
辅助空间: O(B 3/2 )