Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Divisibility with specific ending digits by Dr. Eli Selig
- // This code is CCO
- // https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
- import java.math.BigInteger;
- public class Main {
- public static class MatchResult {
- public final BigInteger firstMatch;
- public final BigInteger stepSize;
- public MatchResult(BigInteger firstMatch, BigInteger stepSize) {
- this.firstMatch = firstMatch;
- this.stepSize = stepSize;
- }
- @Override
- public String toString() {
- return "First match: " + firstMatch + ", repeats every " + stepSize;
- }
- }
- public static MatchResult findMatch(int divisor1, int divisor2, int endingDigits) {
- BigInteger a = BigInteger.valueOf(divisor1);
- BigInteger b = BigInteger.valueOf(divisor2);
- BigInteger d = a.multiply(b).divide(a.gcd(b));
- BigInteger e = BigInteger.valueOf(endingDigits);
- int digits = String.valueOf(endingDigits).length();
- BigInteger m = BigInteger.TEN.pow(digits);
- if (!e.mod(d.gcd(m)).equals(BigInteger.ZERO)) {
- System.out.println("No solution.");
- return null;
- }
- BigInteger n = e;
- while (!n.mod(d).equals(BigInteger.ZERO))
- n = n.add(m);
- return new MatchResult(n, d.multiply(m).divide(d.gcd(m)));
- }
- public static void main(String[] args) {
- MatchResult result = findMatch(567, 33, 567);
- if (result != null)
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment