Junction Numbers in Java
In this article, we'll learn about Junction numbers in Java. Junction Numbers are fascinating mathematical entities distinguished by a unique feature. A Junction Number (N) may be expressed as the sum of a specific value K and its digits for at least two different K values.
A number N is defined as a Junction Number if there exist at least two values of K such that:
N = K+ SumOfDigits(K)
Here, SumOfDigits(K) represents the sum of K's digits.
Example 1:
Input: N = 101
Output: 101 is a Junction Number.
Explanation: K=91 is a feasible combination since 91+ SumOfDigits(9+1)=101.
Another option is K=100, where 100 + SumOfDigits(1+0+0 ) = 101.
As a result, 101 claims to be a junction number.
Example 2:
Input: N = 102
Output: 102 is not a Junction Number.
Explanation: Since we couldn't find any such K, we conclude that 102 is not a junction number.
The numbers that cannot be expressed as K+ SumOfDigits(K) are known as Self Numbers.
As a result, 102 is not a Junction Number.
Example 3:
Input: N = 719
Output: 719 is a Junction Number.
Explanation: K=697 is a feasible combination since 697+ SumOfDigits(6+9+7)=719.
Another option is K=706, where 706 + SumOfDigits(7+0+6 ) = 719.
As a result, 719 claims to be a junction number.
Similarly, we can verify other numbers.
The first few Junction Numbers between 0 and 300 are:101,103, 105, 107, 109, 111, 113, 115, 117, 202, 204, 206, 208 210, 212, 214, 216, 218.
Implementation: Iterative
Let's have a look at how to implement the junction numbers indicated above.
FileName: JunctionNum.java
public class JunctionNum {
// Function to find the sum of digits of a number
static int SumOfDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
// Function to check if a number is a Junction Number
static void checkJunctionNumber(int N) {
int count = 0;
for (int K = 1; K <= N; K++) {
if (K + SumOfDigits(K) == N) {
count++;
if (count >= 2) {
System.out.println(N + " is Junction Number");
return;
}
}
}
System.out.println(N + " is Not a Junction Number");
}
// Driver Code
public static void main(String[] args) {
int N = 101;
checkJunctionNumber(N);
}
}
Output:
101 is Junction Number
Time complexity: The above program uses a loop to run from K =1 to K = N, so the time complexity is O(N).
Space complexity: The space complexity is constant O(1) because no new data structures are employed to scale with the input.
Implementation: Recursive
Using recursion, one can also determine whether they are junction numbers or not. Consider the following program.
FileName: JunctionNum1.java
public class JunctionNum1 {
// Function to find the sum of digits of a number
static int SumOfDigits(int num) {
if (num == 0) {
return 0;
}
return num % 10 + SumOfDigits(num / 10);
}
// Recursive function to check if a number is a Junction Number
static void checkJunctionNumber(int N, int K, int count) {
if (K > N) {
System.out.println(N + (count >= 2 ? " is Junction Number" : " is Not a Junction Number"));
return;
}
if (K + SumOfDigits(K) == N) {
count++;
}
checkJunctionNumber(N, K + 1, count);
}
// Driver Code
public static void main(String[] args) {
int N = 101;
checkJunctionNumber(N, 1, 0);
}
}
Output:
101 is Junction Number
Time complexity: The recursive function is called for each value of K from 1 to N, so the time complexity is O(N).
Space complexity: The recursive call stack results in an O(N) space complexity, If N is very large, the iterative approach might be more efficient in terms of space usage.
Applications:
Junction Numbers can be used to solve a wide variety of mathematical challenges and problems. Identifying these numbers can lead to the discovery of interesting patterns and insights into number theory.
Junction Numbers can be identified and explored using algorithms written in any programming language, including Java. This investigation not only broadens one's comprehension of number theory, but it also prepares the way for discovering the beauty and complexity concealed inside these various mathematical concepts.