Prime Crime 3

2017 \huge{\color{#69047E}{2017}} What is the 201 6 t h prime number ending with 7 ? \text{ What is the } 2016^{th} \text{ prime number ending with 7 ?}


this problem is a part of set Prime Crimes via Computer Science
81817 81847 81937 81737

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Arulx Z
Jan 23, 2016

Miller-Rabin + Brute force

 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
import random
import sys

def prime(n, k = 7):
   """use Rabin-Miller algorithm to return True (n is probably prime)
or False (n is definitely composite)"""
   if n < 6:  # assuming n >= 0 in all cases... shortcut small cases here
      return [False, False, True, True, False, True][n]
   elif n & 1 == 0:  # should be faster than n % 2
      return False
   else:
      s, d = 0, n - 1
      while d & 1 == 0:
         s, d = s + 1, d >> 1
      # Use random.randint(2, n-2) for very large numbers
      for a in random.sample(xrange(2, min(n - 2, sys.maxint)), min(n - 4, k)):
         x = pow(a, d, n)
         if x != 1 and x + 1 != n:
            for r in xrange(1, s):
               x = pow(x, 2, n)
               if x == 1:
                  return False  # composite for sure
               elif x == n - 1:
                  a = 0  # so we know loop didn't continue to end
                  break  # could be strong liar, try another a
            if a:
               return False  # composite if we reached end of this loop
      return True  # probably prime if reached end of outer loop

count = 0
n = 3

while count < 2016:
   if n % 10 == 7:
      count += prime(n)
   n += 2

print n - 2

Moderator note:

Simple standard approach.

Well ... can you explain the logic behind that ?

Zeeshan Ali - 5 years, 4 months ago

Log in to reply

Miller-Rabin test or the brute force part?

Arulx Z - 5 years, 4 months ago

Log in to reply

Yes... please elaborate your logic in that solution.

Zeeshan Ali - 5 years, 4 months ago

Log in to reply

@Zeeshan Ali Miller-Rabin is a probabilistic primality test. I think you should read this and this to get a better idea about it.

Arulx Z - 5 years, 4 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
n=0
a=6
while n<2016:
    b=2
    while b<a:
        if a%b==0:
            b+=a
        else:
            b+=1
    if a==b:
        c=str(a)
        if str(a)[len(str(a))-1]=="7":
            n+=1
            print a
    a+=1

What language is that?

Zeeshan Ali - 5 years, 3 months ago

Log in to reply

python, I did it that way because it was easier than in c++ (I'm getting started in c++)

Hjalmar Orellana Soto - 5 years, 3 months ago

Log in to reply

Oh! I just got confused to see so much underscores ( _ ) :P

Zeeshan Ali - 5 years, 3 months ago

Log in to reply

@Zeeshan Ali oh, sorry, that's because I hadn't found how to write codes here as you do xD May you explain it to me, please?

Hjalmar Orellana Soto - 5 years, 3 months ago

Log in to reply

@Hjalmar Orellana Soto Just folow the way below:

p y t h o n ```python

//code

```

Zeeshan Ali - 5 years, 3 months ago

You can just copy and paste your code between " p y t h o n " a n d " " " ```python " and " ``` "

Zeeshan Ali - 5 years, 3 months ago

Log in to reply

@Zeeshan Ali Untill I could!! Thank you

Hjalmar Orellana Soto - 5 years, 3 months ago

Log in to reply

@Hjalmar Orellana Soto Any time! :)

Zeeshan Ali - 5 years, 3 months ago
Zeeshan Ali
Jan 23, 2016

Here is a C++ code to find whether a number is prime or not.

1
2
3
4
5
6
7
8
    bool is_prime(int N){
        for (int i=2; i<N/2; i++){
            if (N%i==0){
                return false;
            }
        }
        return true;
    }

I leave the further work on you all, if you don't mind :)

Shaun Leong
Jan 21, 2016

A solution using Python 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def isprime(n):
    i=2
    while i*i<=n:
        if n%i==0:
            return False
            break
        else:
            i+=1
    if i*i>n:
        return True
count=0
current=1
n=0
while count < 2016:
    if isprime(10*n+7):
        count+=1
        current=10*n+7
    n+=1
if count==2016:
    print "The number is ", current

That is an admirable efficient effort... :)

Zeeshan Ali - 5 years, 4 months ago

The function to find if a number is prime could be more efficient :)

Zeeshan Ali - 5 years, 4 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...