30
3
Primes are everywhere...
they hide inside Pi
3.1415926535897932384626433832795028841971693993751
Let's get those primes!
The Challenge
Given as input an integer n>0
, find out how many primes are hidden inside the first n
digits of Pi
Examples
For n=3
we should search for primes in [3,1,4]
. There are 2 Primes (3,31)
, so your code should output 2
For n=10
, the first 10 digits are [3,1,4,1,5,9,2,6,5,3]
and your code should output 12
because [2, 3, 5, 31, 41, 53, 59, 653, 4159, 14159, 314159, 1592653]
were hidden (and found!)
Test Cases
input -> output
1->1
3->2
13->14
22->28
42->60
50->93
150->197
250->363
500->895
Rules
Your code must be able to find all primes at least for n=50
Yes, you can hardcode the first 50 digits of Pi
if you like
Entries hardcoding the answers are invalid
This is code-golf.Shortest answer in bytes wins!
6"you can hardcode the first 50 digits of Pi if you like". First problem solved! Now for the golfed primality test on up to 50-digit integers... O_o (This is a nice challenge, but solid math built-ins or libraries are probably required.) – Arnauld – 2017-08-24T23:59:24.223
1First 50 values (thanks to Mathematica):
[1, 2, 2, 3, 4, 8, 9, 9, 9, 12, 12, 12, 14, 21, 24, 25, 25, 28, 28, 28, 28, 28, 28, 28, 32, 33, 33, 39, 39, 42, 44, 44, 44, 44, 44, 44, 44, 48, 51, 54, 60, 60, 64, 72, 74, 79, 82, 88, 88, 93]
– totallyhuman – 2017-08-25T01:01:44.280For math-illiterate languages: would it be against the spirit of the challenge to encode the results instead? – Arnauld – 2017-08-25T01:07:01.203
1@Arnauld the spirit of challenge is... freedom! – None – 2017-08-25T01:09:38.403
3@totallyhuman That sequence not even in OEIS yet! Time for your claim to fame? – Sanchises – 2017-08-25T07:38:58.987
3IMO allowing hardcoding of the first 50 values is detrimental to this challenge. This challenge is basically two parts, 1) try to compress the first 50 values, or 2) actually do the challenge. – JAD – 2017-08-25T08:43:50.053
1@JarkoDubbeldam That is exactly the reason I picked 50. If someone can't "actually do the challenge" or the language he/she knows can't do it, then there is a second chance which costs +50 bytes. Are you actually going to do the challenge or you are here just to downvote? – None – 2017-08-25T12:05:37.270
This has nothing to do with languages not being able to do it, Python for example should be able to properly calculate it, but there is no reason to do so, because hardcoding is that much shorter. – JAD – 2017-08-25T12:08:02.360
2Usually in these kind of challenges, where calculation becomes harder/slower/memory intensive, it is enough for the program to work theoretically, instead of setting an arbitrary cutoff and allowing hardcoding. – JAD – 2017-08-25T12:10:17.597
1Is it me, or are the entries hardcoding the answers against the rules (they only allow "Yes, you can hardcode the first 50 digits of Pi if you like")? – TripeHound – 2017-08-25T15:25:45.887
@TripeHound You are absolutely right! Entries hardcoding the answers are invalid – None – 2017-08-25T15:43:25.397
3
@BillSteihn Updating rules after there are several answers is against the spirit of this website. Have you posted this question in the Sandbox? You would have had feedback really early that hardcoded answers would come in.
– Olivier Grégoire – 2017-08-25T15:58:44.577@BillSteihn also, this should say: distinct primes. – Giuseppe – 2017-08-25T16:26:49.267