CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
31
Task/Digital-root/Java/digital-root.java
Normal file
31
Task/Digital-root/Java/digital-root.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import java.math.BigInteger;
|
||||
|
||||
class DigitalRoot
|
||||
{
|
||||
public static int[] calcDigitalRoot(String number, int base)
|
||||
{
|
||||
BigInteger bi = new BigInteger(number, base);
|
||||
int additivePersistence = 0;
|
||||
if (bi.signum() < 0)
|
||||
bi = bi.negate();
|
||||
BigInteger biBase = BigInteger.valueOf(base);
|
||||
while (bi.compareTo(biBase) >= 0)
|
||||
{
|
||||
number = bi.toString(base);
|
||||
bi = BigInteger.ZERO;
|
||||
for (int i = 0; i < number.length(); i++)
|
||||
bi = bi.add(new BigInteger(number.substring(i, i + 1), base));
|
||||
additivePersistence++;
|
||||
}
|
||||
return new int[] { additivePersistence, bi.intValue() };
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
for (String arg : args)
|
||||
{
|
||||
int[] results = calcDigitalRoot(arg, 10);
|
||||
System.out.println(arg + " has additive persistence " + results[0] + " and digital root of " + results[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue