talltrees


Joined 1 year ago
Homeworks submitted:
Homework comments:
2
0

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 11% complete

Submitted Assignments

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1

This is the first real program I've ever made. I think it's efficient, and also lets the user choose the Nth prime number they wish to find, not just the 1000th.

## This program can find the Nth prime number, as input by the user.

## user input section
import string
import math
goal = raw_input("Enter the Nth prime number you wish to find: ")
while str.isdigit(goal) == False:
    print "You may only enter integers."
    goal = raw_input("Enter the Nth prime number you wish to find: ")
    
goal = int(goal)
testnumcount = 1
numofprimes = 0

## loop through all required test numbers
while numofprimes < goal+1:
    testnum = testnumcount
    divisor = 3
    prime = 1
    looping = True

    ## check to see if individual test number is prime,
    ## skipping all even numbers except 2
    while looping == True:
        if testnum != 2:
            if testnum % 2 == 0:
                divisor += 1
                prime = 0
        if testnum % divisor == 0:
            if testnum != divisor:
                divisor += 1
                prime = 0
        else:
            divisor += 1
        ## conditions for ending the test
        if divisor > math.sqrt(testnum):
            looping = False
        if prime == 0:
            looping = False
    ## what to do if a prime is found
    if prime == 1:
        if numofprimes == goal:
            print testnum,"is the last prime number in a series of",goal
        ##print testnum,"is prime."
        numofprimes += 1
    testnumcount += 1
print "END"

talltrees 1 year ago