-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhugeBook.java
More file actions
73 lines (66 loc) · 2.51 KB
/
hugeBook.java
File metadata and controls
73 lines (66 loc) · 2.51 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.math.BigInteger;
import java.util.*;
public class hugeBook {
public static long pageDigits(long pages) {
// one page at a time
// digits = 0
// for i in 1 to 4000000000:
// digits += # of digits in i
// print digits
//
// 1 to 9: all contain 1 digit. we know there are 9 of them.
// digits += 1 * 9
//
// 10 to 99: all contain 2 digits, we know there are 90 of them
// digits += 2 * 90
//
// 100 to 999: digits += 3 * 900
//
// stops at 583.
// 100 to 583: 3 * 584
// 1 2 3 number of digits is 3
// book that has 10 pages 1, 2, 3, 4, 5, 6, 7, 8, 9, 10: number of digits is 11
// 87495 : 1 .. 9, 10..99, 100..999, 1000..9999, 10000..87495
// 9 2*90 3*900 4*9000 numDigits * (pages - 10^(numDigits-1) + 1)
// count = 0
// number = 87495 : 5 digits
// number = 1: numDigits = 1
//
// int numDigits is num digits in input number
// count = 0
// for (int i = 1; i <= numDigits-1; i++) {
// add i * 9 * 10^(i-1) to count
// how many numbers are there from 10000 to 87495
// A B
// take pages - 10^(numDigits-1) + 1
// length of string is above multiplied by numDigits
//
// add numDigits * (number - 10^(numDigits - 1) + 1)
// }
// [8, 7 , 4, 9, 5, 6, 7 8]
// [9 90 900 9000 90000 900000 9000000 90000000
// 8 digits. 9,999,999 < n <= 99,999,999
// long, double, float, int, char
long count = 0L;
long numDigits = String.valueOf(pages).length();
// BigInteger ten = new BigInteger("10")
for(int i = 1; i <= numDigits - 1; i++){
long powInt = (long) Math.pow(10l, i - 1) ;
// if(pages == powInt) {
// long nine = i * 9;
// count += powInt * nine;
// } else{
// count += i * (pages - powInt);
// break;
// }
// System.out.println(i);
// System.out.println(powInt);
count += i * 9 * powInt;
}
count += numDigits * (pages - (long) Math.pow(10,numDigits - 1) + 1);
return count;
}
public static void main(String[] args) {
System.out.println(pageDigits(100));
}
}