0 Replies - 1652 Views - Last Post: 04 April 2012 - 10:18 PM

#1 raspinudo   User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Printing Primes

Posted 04 April 2012 - 10:18 PM

Description: A function to print primes up to a given ceiling. It could be easily modified to store them in a list or whatever data structure nec.
import math

def primes(x):
    temp = ''
    for z in range(2,x):
        prime = True
        for i in range(2,int(math.sqrt(z)+1)):
            if z%i == 0:
                prime = False
                break
        if prime:
            temp = temp + "[" +  str(z) + "]"
    return temp

print primes(int(raw_input("Specify a ceiling: ")))



Is This A Good Question/Topic? 0
  • +

Page 1 of 1