Python Forum
Pairs of multiplied prime number--->N
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pairs of multiplied prime number--->N
#11
This code was close to working.
def prime_eratosthenes(lim):
    prime_list = []
    for i in range(2, lim+1):
        if i not in prime_list:
            print (i, end = ' ')
            for j in range(i*i, lim+1, i):
                prime_list.append(j)
    return prime_list
  
print(prime_eratosthenes(10))
This prints primes in the correct range, but it does not return these values. Why not? Why are you returning prime_list? This is a list of values that are used to identify primes, not a list of primes. In fact, prime_list is a list of numbers that are not primes as indicated by "if i not in prime_list:" being the test to determine if i is prime.
def prime_eratosthenes(lim):
    primes = []
    not_primes = []
    for i in range(2, lim+1):
        if i not in not_primes:
            primes.append(i)
            for j in range(i*i, lim+1, i):
                not_primes.append(j)
    return primes
  
print(prime_eratosthenes(10))
Frankduc likes this post
Reply


Messages In This Thread
Pairs of multiplied prime number--->N - by Frankduc - Jan-14-2022, 07:22 PM
RE: Pairs of multiplied prime number--->N - by deanhystad - Jan-15-2022, 06:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pairs Trading Simulation Kiitoos 0 262 Feb-19-2024, 08:27 PM
Last Post: Kiitoos
  Prime number detector Mark17 5 880 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 502 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Extracting unique pairs from a data set based on another value rybina 2 2,347 Feb-12-2021, 08:36 AM
Last Post: rybina
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,968 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  check if the number is a prime integer atlass218 5 3,008 Sep-26-2019, 07:58 AM
Last Post: atlass218
  Key value pairs assistance UtiliseIT 2 2,673 May-09-2019, 09:26 AM
Last Post: UtiliseIT
  Creating a program to look for the largest prime number of a number Wikki14 4 3,943 Sep-08-2018, 12:30 AM
Last Post: Skaperen
  Create random pairs Dennisp44 3 8,082 Jun-02-2018, 05:51 AM
Last Post: buran
  Unexpected result in simple prime number example jackhj 2 3,052 Apr-20-2018, 01:48 AM
Last Post: jackhj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020