×

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

  1. Declare a number X.
  2. Find all the divisors of the given number X.
  3.  Verify whether the number x is untouchable if the sum of all its proper divisors equals x, or whether it is not.
  4. 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

Related Topics

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.