| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | 230726T164854Z | FarSeenN | |
| 003 | Python | 150911T230419Z | Zach Gat |
Mathematica
Print[""<>IntegerString[MaximalBy[Tally[Join@@IntegerDigits[Prime[Range[PrimePi[#1]+1,PrimePi[#2]]]]],Last][[;;,1]]&@@FromDigits/@StringSplit@InputString[]]]
Time Specs
Using Mathematica's builtin timer, AbsoluteTiming, I got these times for each listed range.
f=MaximalBy[Tally[Join@@IntegerDigits[Prime[Range[PrimePi[#1]+1,PrimePi[#2]]]]],Last][[;;,1]]&
Grid[Table[{1,10^x+1,AbsoluteTiming[f[1,10^x+1]][[1]]},{x,1,7}]]
Returns:
1 11 0.0003388
1 101 0.0003645
1 1001 0.0016222
1 10001 0.0040902
1 100001 0.0267737
1 1000001 0.211297
1 10000001 4.55387
1 100000001 43.9889
It can go over a range of a hundred million numbers in under a minute.
Usage
wolframscript -f primedigitcount.wls
? 21 41
3
wolframscript -f primedigitcount.wls
? 51 89
7
wolframscript -f primedigitcount.wls
? 201 501
3
Ungolfed
Takes the space-seperated input and converts it to two integers
a = FromDigits/@StringSplit@InputString[]
PrimePi is the inverse function of Prime, so this gets all the primes between the two a's. The +1 is that if the operand is not prime, it gets the first prime below it, but we want a prime above the smaller number.
b = Prime[Range[PrimePi[#1]+1,PrimePi[#2]]&@@a
Turns every number in the list into a list of it's digits, and then concatenates all the lists into one list of all the digits
c = Join@@IntegerDigits[b]
Mathematica has a nice built-in for turning a list into unique items and their counts.
d = Tally[c]
Takes a list of the item (or items) who has the largest count, and strips off the tally.
e = MaximalBy[d,Last][[;;,1]]
Converts every number in the list to a string, concatenates them, and prints it.
Print[""<>IntegerString[e]]
Conjecture:
In hope that somebody more mathematically inclined could find this to be an O(n) problem, I leave you this conjecture:
If the first number is kept as 1, and the 2nd number tends to be large (n>100), the result will be no more than two of 1, 3, or 7.
Python 3
import collections
p=lambda n:n>1 and all(n%i for i in range(int(n**0.5),1,-1))
print(collections.Counter(''.join(map(str,filter(p,range(*map(int,raw_input().split(' '))))))).most_common(1)[0][0])
Probably not the best way it can be done, but a start. (196 bytes)
Time Specs
Using Python's builtin code timer module, timeit, I got these times for each listed range.
(1, 11): 0.000084381103520
(1, 101): 0.000338881015778
(1, 1001): 0.003911740779880
(1, 10001): 0.071979818344100
(1, 100001): 1.785329985620000
(1, 1000001): 54.977741956700000
(1, 10000001): 1700.231099130000000
It can go over a range of a million numbers in under a minute.
Usage
$ python test.py
21 41
3
$ python test.py
51 89
7
$ python test.py
201 501
3
Ungolfed
import collections
def is_it_prime(n):
if n > 1:
if all(n % i for i in range(int(n ** 0.5), 1, -1)):
return True
return False
start, stop = raw_input.split(' ', 1)
primes = ""
for p in range(int(start), int(stop)):
if is_it_prime(p):
primes += str(p)
counter = collections.Counter(primes).most_common(1)
print(counter[0][0])