Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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