Primes Numbers Using Python Threads

Asked

Viewed 478 times

2

well I need to create a program in python using threads that find the largest number of primes in 60 seconds, but I cannot understand the use of threads for it:`

import time
import math




def is_prime(number):
    number = int(number)
    if number == 2:
        return True
    num_sqrt = int(math.sqrt(number))
    if number:
        if number % 2 == 0:
            return False
        for i in range(3, num_sqrt+1, 2):
            if number % i == 0:
                return False

    return True

def func_Prime(i):
    i = 0
    numero = 3
    print 'PROGRAMA QUE VERIFICA E MOSTRA QUAIS OS NÚMEROS PRIMOS.'
    ini = time.time()
    final = 0
    while(final < 1):

        numero = numero+1
        resposta = is_prime(numero)
        if resposta:

            i = i+1
            primo = numero
            fim = time.time()
            final = fim - ini
    return primo, i
  • I did some tests here, and I changed some things in its function, now it is returning the amount of prime numbers given a time as parameter and it also returns the last prime number. I don’t know exactly what you want, because you need to compare the values, but as your function returns many prime numbers it is difficult to check one by one, so I assumed as a basis the last number returned by it checking if it is prime.

  • The time is used to measure the execution time, in this case it is being applied to determine how long the function will be calculating prime number.

  • the function of this work is to use the threads to find as many primes as possible, the code returns the last prime number found and the quantity found in 60 seconds

  • The time.time() it is not a thread if I am not mistaken, have to consult the documentation.

  • I’m confused about what you want and what your problem is.

1 answer

1


I think the individual wants to run two features at the same time. That is, counting all numbers that are primes at 60 s. While the program counts primes from 2 to x, it also counts the limited time by 60 s. When the time reaches 60 s the prime counter stops. Also looking for how to use threads.

** It was supposed to be a comment. I’m sorry. I’m on my cell phone. **

Browser other questions tagged

You are not signed in. Login or sign up in order to post.