-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChampernowneCount.java
More file actions
32 lines (30 loc) · 938 Bytes
/
Copy pathChampernowneCount.java
File metadata and controls
32 lines (30 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
/*
* Modular math - (a*b)modd (amodd*bmodd)modd
* 12345 = 1234 * 10 + 5
* 123456789101112 = 1234567891011 * 100 + 12
* When you get to 3 digit numbers = * 1000 + x
* 4 digits = * 10000 + x
*/
class ChampernowneCount{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int k = sc.nextInt();
if(k == 1){
System.out.println(size);
return;
}
long curr = 1; long rem = 1; int count = 0; int exp = 1;
for (int i = 2; i <= size; i++) {
exp = (""+i).length();
//get remainder, and add i
//if divisible by k, count++
//as i increases digits, increase power of 10
curr = rem * (int)Math.pow(10,exp) + i;
if(curr%k==0) count++;
rem = curr%k;
}
System.out.println(count);
}
}