Guest User

Number Theory for Maids

a guest
Aug 2nd, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | Source Code | 0 0
  1. // Divisibility with specific ending digits by Dr. Eli Selig
  2. // This code is CCO
  3. // https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
  4.  
  5. import java.math.BigInteger;
  6.  
  7. public class Main {
  8.  
  9.     public static class MatchResult {
  10.         public final BigInteger firstMatch;
  11.         public final BigInteger stepSize;
  12.         public MatchResult(BigInteger firstMatch, BigInteger stepSize) {
  13.             this.firstMatch = firstMatch;
  14.             this.stepSize = stepSize;
  15.         }
  16.         @Override
  17.         public String toString() {
  18.             return "First match: " + firstMatch + ", repeats every " + stepSize;
  19.         }
  20.     }
  21.  
  22.     public static MatchResult findMatch(int divisor1, int divisor2, int endingDigits) {
  23.         BigInteger a = BigInteger.valueOf(divisor1);
  24.         BigInteger b = BigInteger.valueOf(divisor2);
  25.         BigInteger d = a.multiply(b).divide(a.gcd(b));
  26.         BigInteger e = BigInteger.valueOf(endingDigits);
  27.         int digits = String.valueOf(endingDigits).length();
  28.         BigInteger m = BigInteger.TEN.pow(digits);
  29.  
  30.         if (!e.mod(d.gcd(m)).equals(BigInteger.ZERO)) {
  31.             System.out.println("No solution.");
  32.             return null;
  33.         }
  34.  
  35.         BigInteger n = e;
  36.         while (!n.mod(d).equals(BigInteger.ZERO))
  37.             n = n.add(m);
  38.         return new MatchResult(n, d.multiply(m).divide(d.gcd(m)));
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.         MatchResult result = findMatch(567, 33, 567);
  43.         if (result != null)
  44.             System.out.println(result);
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment