topic
3 If you meet and divisible by 3, then the number is divisible by 3 in decimal.
5 In the Hex also satisfy this rule.
Given number k, find the number hexadecimal (hex range within 1e18) meet at this rule, to find a free output is -1.
answer
May draw law can be found, i.e., k is coprime to find the number of x, i.e. the lower x decimal meet the above rule.
Related
Greatest common divisor: Euclidean algorithm (also known as the Euclidean algorithm)
- Euclid theorem:
gcd(a, b) = gcd(b , a mod b)
for positive integers a, b. - Wherein a, b size does not matter. When the value is less than a value b, the recursive call algorithm can be exchanged over the values of a and b.
Code
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long k = sc.nextInt();
long x = k + 1;
for (; x <= 1e18; ++x) {
if (gcdGreaterThanOne(k, x)) {
System.out.println(x);
break;
}
}
if (x > 1e18) {
System.out.println(-1);
}
}
public static boolean gcdGreaterThanOne(long num1, long num2) {
return gcd(num1, num2) == 1;
}
private static long gcd(long num1, long num2) {
while (num2 != 0) {
long r = num1 % num2;
num1 = num2;
num2 = r;
}
return num1;
}
}