-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path387.Harshad Numbers.java
125 lines (105 loc) · 4.36 KB
/
387.Harshad Numbers.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
A Harshad or Niven number is a number that is divisible by the sum of its digits.
201 is a Harshad number because it is divisible by 3 (the sum of its digits.)
When we truncate the last digit from 201, we get 20, which is a Harshad number.
When we truncate the last digit from 20, we get 2, which is also a Harshad number.
Let's call a Harshad number that, while recursively truncating the last digit, always results in a Harshad number a right truncatable Harshad number.
Also:
201/3=67 which is prime.
Let's call a Harshad number that, when divided by the sum of its digits, results in a prime a strong Harshad number.
Now take the number 2011 which is prime.
When we truncate the last digit from it we get 201, a strong Harshad number that is also right truncatable.
Let's call such primes strong, right truncatable Harshad primes.
You are given that the sum of the strong, right truncatable Harshad primes less than 10000 is 90619.
Find the sum of the strong, right truncatable Harshad primes less than 1014.
import java.math.BigInteger;
import java.util.Calendar;
public class Problem387 {
//Using long, less then power 15
long ebi = 10000L*10000L*10000*1000L;
long tothn =0;
boolean[] bprime = new boolean[10000000 + 1];
int[] prime = new int[664579];
private void primecal() {
int currpos = 0;
bprime[0] = bprime[1] = true;
for (int i = 0; i < bprime.length; i++) {
if (!bprime[i]) {
prime[currpos++] = i;
for (int j = 2 * i; j < bprime.length; j += i) {
bprime[j] = true;
}
}
}
}
public void harshad() {
primecal();
for (int i=1;i<=9;i++)
harshadprimenumber(i,i);
System.out.println("totalothn );
}
private void harshadprimenumber(long sbi, int sum) {
//System.out.println(sbi + "\t" + sum + "\t" +(sbi%sum));
if (sbi%sum!=0 || sbi*10>ebi) return;
if(isPrime(sbi/sum)) {
for (long i=sbi*10+1;i<=sbi*10+9;i+=2){
if (isPrime(i)) {
//System.out.println(i+"\t"+sum tothn+=i;
}
}
}
for (int i=0;i<=9;i++)
harshadprimenumber(sbi*10+i, sum+i);
}
private boolean isPrime(long n) {
if (n < bprime.length) {
return !bprime[(int) n];
}
int lim = (int) (Math.sqrt(n) + 1);
for (int i = 0; i < prime.length; i++) {
if (n % prime[i] == 0) {
return false;
}
if (prime[i] > lim) {
return true;
}
}
return true;
}
//Using long, more then power 15.
BigInteger biebi = BigInteger.valueOf(10).pow(15);
//52735 milliseconds
BigInteger bitothn = BigInteger.ZERO;
public void BIharshad() {
for (int i=1;i<=9;i++)
BIharshadprimenumber(BigInteger.valueOf(i),BigInteger.valueOf(i));
System.out.println("totalitothn );
}
private void BIharshadprimenumber(BigInteger sbi, BigInteger sum) {
if ((sbi.mod(sum)).compareTo(BigInteger.ZERO)!=0 || (sbi.multiply(BigInteger.valueOf(10)).compareTo(biebi)>0)) return;
if(sbi.divide(sum).isProbablePrime(10000)) {
for (BigInteger i=(sbi.multiply(BigInteger.valueOf(10))).add(BigInteger.ONE);i.compareTo((sbi.multiply(BigInteger.valueOf(10))).add(BigInteger.valueOf(9)))<=0;i=i.add(BigInteger.valueOf(2{
if(i.isProbablePrime(10000)) {
//System.out.println("\t);
bitothn= bitothn.add(i);
}
}
}
for (int i=0;i<=9;i++)
BIharshadprimenumber((sbi.multiply(BigInteger.valueOf(10))).add(BigInteger.valueOf(i)),sum.add(BigInteger.valueOf(i)));
}
/**
* @param args
*/
public static void main(String[] args) {
long start = 0, end=0;
Problem387 p = new Problem387();
start =(Calendar.getInstance()).getTimeInMillis();
p.harshad ();
end =(Calendar.getInstance()).getTimeInMillis();
System.out.println(start + " - " + end + " - " +(end-start));
start =(Calendar.getInstance()).getTimeInMillis();
p.BIharshad ();
end =(Calendar.getInstance()).getTimeInMillis();
System.out.println(start + " - " + end + " - " +(end-start));
}
}