Untouchable Number in Java
If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from the OEIS.
It is a positive integer that cannot be represented as the sum of any positive integer's suitable divisors, unlike the untouchable number.
Properties
- A number that cannot be divided is not a perfect number.
- All untouchable numbers are composite, with the exception of 2 and 5.
It is not an impossible number since the number 4 equals the total of the proper divisors of the number 9, i.e., 1+3 = 4.
Because it is not equal to the appropriate sum of any positive integer's divisors, the number 5 is untouchable. The only method to write 5 is as 1+4 in other words (there are no other ways). A number is equally divided by the digits 4 and 2. As a result, it is impossible to add 1+4 to any legitimate divisor.
The only odd untouchable number, as far as we can tell, is 5.
Steps to Solve an Untouchable Number
- Declare a number X.
- Find all the divisors of the given number X.
- Verify whether the number x is untouchable if the sum of all its proper divisors equals x, or whether it is not.
- Check if i is the divisor of X or not:
- If yes, add x to the variable a.
- Else, add sum, x (divisor), and the resultant of (a/x) together.
- At last, add 1 to the variable a.
Example:
The number 4 is not untouchable just because it is equal to the sum of the proper divisors of the number 9, for example, 1 + 3. The number 5 is untouchable because it does not equal the sum of the proper divisors of any positive integer.The only way to express 5 as the sum of distinct positive integers up to and including 1 is to write it as 5 = 1 + 4, but since 2 also divides by 4, it is impossible for 1 + 4 to equal the sum of all of a number's legitimate divisors (since the list of factors would have to contain both 4 and 2).
// java program to demonstrate untouchable number
import java.util.Scanner;
public class solution
{
//function determines the supplied number is an untouchable number or not.
static booleanisUntouchable(int x)
{
for(int n = 1; n<= 2 * x; n++)
{
if (sumOfDivisors(n) == x)
return false;
}
return true;
}
static int sumOfDivisors(int a)
{
//stores the sum of the divisors
int a = 0;
//finds all the divisors of the number
for(int n = 2; n<= Math.sqrt(a); n++)
{
// n is divisor of num
if (a % n == 0)
{
//if equal, add only once, else add both
if (n == (a / n))
a = a + n;
else
a = a + (n + a / n);
}
}
//1 is added to the variable sum because 1 is also a divisor
return (a + 1);
}
//driver code
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("given the number: ");
int a =s.nextInt();
//function calling
if (isUntouchable(a))
System.out.println("it’s an untouchable number. ");
else
System.out.println(" it’s not an untouchable number. ");
}
}
Output:
Given the number: 2
It’s an untouchable number.
given the number: 25
It’s not an untouchable number.
// C++ program to demonstrate untouchable number
#include <bits/stdc++.h>
using namespace std;
// Function to determine the sum of all numeral appropriate divisors
int divSum(int a)
{
// the total of the divisors' sum
int res = 0;
// Find all divisors of num
for (int n = 2; n<= sqrt(a); n++) {
// If 'n' is divisor of 'num'
if (a % n == 0) {
// Add it once if the two divisors are the same; otherwise, add them both.
if (n == (a / n))
res += n;
else
res += (n + a / n);
}
}
// Given that 1 is also a divisor, add 1 to the result.
return (res + 1);
}
// Checking whether X is an untouchable number function
bool isUntouchable(int x)
{
for (int n = 1; n<= 2 * x; n++) {
if (divSum(n) == x)
return false;
}
return true;
}
// Driver Code
int main()
{
// Given Number X
int X = 52;
// Function Call
if (isUntouchable(x))
cout<< "Yes";
else
cout<< "No";
}
Output:
Yes