-
Notifications
You must be signed in to change notification settings - Fork 13
/
Math_RepetitiveAdditionOfDigits.java
88 lines (70 loc) · 2.66 KB
/
Math_RepetitiveAdditionOfDigits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
// this problem has two variants - one in GFG and other one in HackerRank
// Both the variant's solution is here.
// https://practice.geeksforgeeks.org/problems/repetitive-addition-of-digits/0
// https://www.geeksforgeeks.org/finding-sum-of-digits-of-a-number-until-sum-becomes-single-digit/
// https://www.hackerrank.com/challenges/recursive-digit-sum/problem
// https://www.hackerrank.com/challenges/recursive-digit-sum/forum/comments/523856 (BEST solution using Java 8)
// http://applet-magic.com/digitsummod9.htm (a good read)
// https://www.youtube.com/watch?v=kHzcrbKHFbw
class Math_RepetitiveAdditionOfDigits {
// https://www.hackerrank.com/challenges/recursive-digit-sum/problem
// below is the solution of above problem
static int superDigit(String n, int k) {
BigInteger sum = BigInteger.valueOf(k).multiply(new BigInteger(n));
return digitSum(sum).intValue();
}
static BigInteger digitSum(BigInteger n) {
if(n.equals(BigInteger.ZERO)) return BigInteger.ZERO;
return (n.mod(BigInteger.valueOf(9)).equals(BigInteger.ZERO))
? BigInteger.valueOf(9) : n.mod(BigInteger.valueOf(9));
}
// https://www.hackerrank.com/challenges/recursive-digit-sum/forum/comments/523856
static int superDigitUsingJava8 (String n, int k) {
n = n.chars().mapToLong(Character::getNumericValue).sum() * k + "";
return (n.length() > 1) ? superDigit(n, 1) : Character.getNumericValue(n.charAt(0));
}
//-------------------------------------------------------------------------------------
// logic is If N is multiple of 9 then the answer is 9 else answer is N mod 9
private static int getDigitSum(int n) {
if(n == 0) return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
int n = Integer.parseInt(br.readLine());
System.out.println(getDigitSum(n));
}
}
/*public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0) {
int n = Integer.parseInt(br.readLine());
int sum = 0;
int d = 0;
while(n!=0) {
d = n % 10;
sum += d;
n /= 10;
if((sum > 9) & (n==0)) {
n = sum;
sum = 0;
}
}
System.out.println(sum);
}
}*/
}
//one line solution
// If N is multiple of 9 then the answer is 9 else answer is N mod 9
/*
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
*/