ReDestroyDeR

simple base conversion task

Oct 3rd, 2021
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.math.BigInteger;
  2.  
  3. /**
  4.  * @author Daniil Shreyder
  5.  * Date: 16.06.2021
  6.  */
  7.  
  8. public class Test {
  9.     public static void main(String[] args) {
  10.         BigInteger inputBase10 = BigInteger.valueOf(2)
  11.                 .multiply(BigInteger.valueOf(7).pow(2))
  12.                 .add(BigInteger.valueOf(42)
  13.                         .multiply(BigInteger.valueOf(49).pow(49)))
  14.                 .add(BigInteger.valueOf(-357));
  15.  
  16.         System.out.println(inputBase10);
  17.         final BigInteger base = BigInteger.valueOf(7);
  18.         BigInteger inputBase7 = convertToBase(inputBase10, base);
  19.         System.out.println(inputBase7);
  20.  
  21.         System.out.println("Number of 6: " +
  22.                 inputBase7.toString()
  23.                         .chars()
  24.                         .filter(character -> character == '6')
  25.                         .count());
  26.     }
  27.  
  28.     public static BigInteger convertToBase(BigInteger inputBase10, BigInteger base) {
  29.         int counter = 0;
  30.  
  31.         BigInteger inputBaseX = BigInteger.ZERO;
  32.  
  33.         while (!inputBase10.equals(BigInteger.ZERO)) {
  34.             inputBaseX = inputBaseX.add(inputBase10.mod(base).multiply(BigInteger.valueOf(10).pow(counter)));
  35.             inputBase10 = inputBase10.divide(base);
  36.             counter++;
  37.         }
  38.  
  39.         return inputBaseX;
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment