1

Ok so basically I had this problem and searched the web for it, and stack overflow there is no code with the new update of python 3.0 that does this on stack overflow and many other site, so as my first post I want to contribute to stackoverflow, because I finally found a solution

so first you want

import random
from math import floor
from math import sqrt

These are important because you'll see I we'll need these specific tidbits later

Firstly lets make a function that checks for or validates wither or not something is prime

def pkf(n):
if n%2==0:
    return False
else:
    if sqrt(n)-floor(sqrt(n))==0:
        return False
    else:
        for i in range(3,floor(sqrt(n)-1),2): 
            if n%i==0: 
                return False
        if n<=2:
            return False 
        else: return True

So let me break this down and explain step by step how and why this works with maximum optimization

def pkf(n):
if n%2==0: 
    return False
else:

The point of starting with this is that if the input is divisible by two we can quickly write it off with will probably be about 50% of the inputs, and I use an if else structure for further optimization so we dont have to run through extra code when the input is already invalid

if sqrt(n)-floor(sqrt(n))==0:

If there is a perfect square root we can eliminate the prime possiblilty

for i in range(3,floor(sqrt(n)-1),2): if n%i==0: return False

We start from 3, although 1, and 2 are prime they are not the size we want Because we filtered out all even numbers, all non prime odd numbers when having a rounded down square root if any factor exists it exists between its square root and bellow

We end at the rounded down square root of the imputed number, again for optimization, all We then move in steps of 2 because we started at 3 which was odd, eliminating half the necessary computations

            if n<=2:
            return False 
        else: return True

Anything less then and including 2 is technical a prime numberhowever that is not the scale of the numbers we want this can be included in the beginning however it is extra compute power is highly unlikely that your min value is set to 0 especially if used for encryption

full code so far

def pkf(n):
if n%2==0: #if it is devisable by two we can quickly write it off and I use an if else structure for optumisation
    return False
else:
    if sqrt(n)-floor(sqrt(n))==0:
        return False
    else:
        for i in range(3,floor(sqrt(n)-1),2): 
            if n%i==0: 
                return False
        if n<=2:
            return False 
        else: return True

Ok now we need 2 variables, a minimum and maximum value for the prime number, For encryption larger numbers is better

mini=1e7
mx=1e11

I used these variable names for minimum and maximum respectively because I'm to lazy to type out the full variable names, and max and min are python parameters so that wouldn't have ended up well

def genPrime():
    n=random.randint(mini,mx)
    while not pkf(n):
        n=random.randint(mini,mx)
    print(n)

Anyway this will help you speedy selectively brute force possibilities with the high optimization, here is the complete code

import random
from math import floor
from math import sqrt

mini=1e7
mx=1e11

def pkf(n):
    if n%2==0: #if it is devisable by two we can quickly write it off and I use an if else structure for optumisation
        return False
    else:
        if sqrt(n)-floor(sqrt(n))==0:
            return False
        else:
            for i in range(3,floor(sqrt(n)-1),2): 
                if n%i==0: 
                    return False
        if n<=2:
            return False 
        else: return True
def genPrime():
    n=random.randint(mini,mx)
    while not pkf(n):
        n=random.randint(mini,mx)
    print(n)
genPrime()

Thanks for reading my artical, I hope to make a few more of these because I understand the struggle, I know how stackoverflow is notorious for taking down posts of new users, but if they dont I am hoping to build a step by step instruction on how to build RSA encryption for now so that you guys dont have to worry, I know its though to get good materiel so I am making it available for you guys, Happy coding!

  • This is more suited for a personal blog than a Q&A site like SO. You can link your personal site in your profile, however. Voting to close this. – DocDriven Dec 12 '18 at 11:31
  • I really appreciate your effort, but SO is a Q&A site and this is not a question. Please consider splitting your post in two parts: a question and an answer, otherwise it may get closed as off-topic and deleted. – t.m.adam Dec 12 '18 at 12:22
  • This would be a perfect fit for [Code Project](https://www.codeproject.com/) I think. That site has some very good articles (and, for the subject of crypto a few very bad ones with many upvotes as well, of course, but yeah). – Maarten Bodewes Dec 13 '18 at 04:05
  • I'm voting to close this question as off-topic because this isn't a question – Maarten Bodewes Dec 13 '18 at 04:05
  • 1
    @t.m.adam cool I'll ask then post – Kartar Singh Dec 14 '18 at 05:24
  • Good choice! You'll find many articles formated as Q&A on this site. For example, see coldspeed's great post: [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101), with 2.6 K views, many upvotes and a 500 bounty! – t.m.adam Dec 14 '18 at 11:30

0 Answers0