How to resolve the algorithm Safe primes and unsafe primes step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Safe primes and unsafe primes step by step in the Python programming language

Table of Contents

Problem Statement

Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Safe primes and unsafe primes step by step in the Python programming language

The provided Python code generates lists of safe prime numbers and unsafe prime numbers up to a specified limit (n). Prime numbers are numbers greater than 1 that have no factors other than 1 and themselves. Safe primes are prime numbers such that (p - 1)/2 is also a prime number, while unsafe primes are prime numbers that do not meet this criterion.

Here's a step-by-step explanation of the code:

  1. Initialization:

    • It initializes empty lists primes, sp (for safe primes), and usp (for unsafe primes).
    • It sets the limit n to 10,000,000.
  2. Generate Prime Numbers:

    • The code uses a simple prime sieve to generate a list of prime numbers up to the limit n.
    • Starting with the number 3 (since 2 is prime and already added), it iterates over odd numbers up to n.
    • For each odd number i, it checks if it's divisible by any of the primes in the primes list. If it's not divisible, then i is a prime and is added to the primes list.
  3. Identify Safe Primes:

    • After generating the list of prime numbers, it identifies safe primes.
    • It checks if (i - 1) / 2 is also a prime for each prime number i.
    • If it is, i is added to the sp list as a safe prime. Otherwise, it's added to the usp list as an unsafe prime.
  4. Print Results:

    • Finally, the code prints the details about safe and unsafe primes below the limit n.
    • It prints the first 35 safe primes, the count of safe primes below 1,000,000 and 10,000,000, the first 40 unsafe primes, and the count of unsafe primes below 1,000,000 and 10,000,000.

Source code in the python programming language

primes =[]
sp =[]
usp=[]
n = 10000000
if 2<n:
    primes.append(2)
for i in range(3,n+1,2):
    for j in primes:
        if(j>i/2) or (j==primes[-1]):
            primes.append(i)
            if((i-1)/2) in primes:
                sp.append(i)
                break
            else:
                usp.append(i)
                break
        if (i%j==0):
            break

print('First 35 safe primes are:\n' , sp[:35])
print('There are '+str(len(sp[:1000000]))+' safe primes below 1,000,000')
print('There are '+str(len(sp))+' safe primes below 10,000,000')
print('First 40 unsafe primes:\n',usp[:40])
print('There are '+str(len(usp[:1000000]))+' unsafe primes below 1,000,000')
print('There are '+str(len(usp))+' safe primes below 10,000,000')


  

You may also check:How to resolve the algorithm Concurrent computing step by step in the Prolog programming language
You may also check:How to resolve the algorithm Range consolidation step by step in the Raku programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the Ada programming language
You may also check:How to resolve the algorithm Update a configuration file step by step in the Python programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the AWK programming language