views:

221

answers:

6

So, I have a very shotty background in programing and I have, for a very long time, attempted to learn through various lessons, though, no matter how good the lesson or tutorial is said to be, it always loses me at some point. For example the CarlH lectures lost me at around unit 5 part 1 and 2 where it jumped the shark and neglected to say how to run, compile, execute code. I got confused and decided to try something else. I tried Invent your own computer games with python though it lost me at around the hangman chapter. I have also tried other things, though there is always that point where it goes "Here, it is easy! Try this...!" and of course it is something that in no way I understand. I am now doing the MIT opencourse thing, and already the second assignment, I feel it has left me out in the cold. http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset1a.pdf If anybody could help me. I don't even know where to begin. Thanks. Hopefully this won't be another ones where it jumps the shark.

The specifics of it, are to write something that can calculate the 1000th prime number. We only know about the print, ==, =, 1=,if, else, elif, while, %, -,+,*,/, commands I think. We also don't yet know about importing libraries.

My Idea of how it would work is to take an odd number and try to divide it by, 3,4,5,6,7,8,9 and if %n !=0, then add a number to NumberofPrimes variable starting with 11 as the base of the tests, and assigning it a base value of 4 at the base of NumberofPrimes, though I don't know if that is even right, because I wouldn't know how to display the 1000th prime number.

Am I close?

The latest incarnation of it is as follows:

##calculate the 1000th prime number
potprime = 3
numberofprime = 1
cycle = if potprime%3 = 0:
            break
        if potpimre%4 = 0:
            break
        if potprime%5 = 0:
            break
        if potprime%6 = 0:
            break
        if potprime%7 = 0:
            break
        if potprime%8 = 0:
            break
        if potprime%9 = 0:
            break
        numberofprime + 1
        potprime + 1

if potprime%2 == 0:
    potprime = potprime + 1
if potprime != 0:
    cycle

Where exactly am I going wrong? Walk me through it step by step. I really want to learn it, though I feel like I am just being left out in the cold here.

At this point, it would be more beneficial for me to see how a proper one could be done rather than doing this. I have been working for 3 hours and have gotten nowhere with it. If anybody has a solution, I would be more than happy to look at it and try to learn from that.

A: 

Your math is failing you. A prime number is a number that has 2 divisors: 1 and itself. You are not testing the numbers for primality.

Enemy of the State
I am though, I am checking if it is even or odd first, then if it is odd, I (trying and failing) to see if it is divisible by 3-8, and if it is, then I add a number to the number of prime numbers and the potential prime. How is that not it?
AB_as_BC
A prime number is one that is NOT divisible by any numbers other than 1 and itself. Your logic seems backwards (returning numbers which ARE divisible by another number not 1 or itself).
Keith Randall
I think he mistyped here because in his psudocode he is breaking the loop if the number is divisible by the other numbers, and recording it if it is not.
xnine
+1  A: 

For one thing, I'm pretty sure that in Python, if you want to have an if statement that tests whether or not A = B, you need to use the == operator, rather then the =.

For another thing, your algorithm would consider the number 143 to be prime, even though 143 = 11 * 13

You need keep track of all the prime numbers that you have already computed - add them to an array. Use that array to determine whether or not a new number that you are testing is prime.

Vladislav
How do I do this? I don't understand anything when it comes to programing at this point really.
AB_as_BC
My bad, you don't yet know about arrays. In that case, you should use the simplest algorithm that you can think of to test for the primality of a number. Repeat it on new numbers until you found 1000 primes.
Vladislav
+2  A: 

It seems to me that you are jumping into the deep-end after deciding the kiddy-pool is too deep. The prime number project will be assignment 2 or 3 in most beginning programming classes, just after basic syntax is covered. Rather than help you with the algorithm (there are many good ones out there) I'm going to suggest that you attempt to learn syntax with the python shell before you write long programs, since debugging a line is easier than debugging an entire program. Here is what you wrote in a way that will actually run:

count = 4
n = 10                        #I'm starting you at 10 because your method 
                              #says that 2, 3, 5, and 7 are not prime
d = [2, 3, 4, 5, 6, 7, 8, 9]  #a list containing the ints you were dividing by

def cycle(n):                 #This is how you define a function
    for i in d:               #i will be each value in the list d
        if not n%i:           #this is equal to if n%i == 0
            return 0          #not prime (well, according to this anyway)
    return 1                  #prime

while count < 1000:
    count += cycle(n)         #adds the return from cycle to count
    n += 1

print n - 1

The answer is still incorrect because that is not how to test for a prime. But knowing a little syntax would at least get you that wrong answer, which is better than a lot of tracebacks.

(Also, I realize lists, for loops, and functions were not in the list of things you say you know.)

xnine
Not really *helpful*, but you do have a point and a nice metaphor ;-)
THC4k
I could just give him the algorithm, but Google could do that as well.
xnine
4, 6, 8 and 9 are prime numbers though.
celenius
@celenius - 2*2=4. not prime. 2*3=6. not prime. 2*4=8. not prime. 3*3=9. not prime. A prime number is one that is only divisible by 1 and itself
xnine
sorry, I meant to say that they are NOT prime numbers and should not be included in the original list. Hence d = [2,3,5,7] to begin, and subsequent primes should be appended to this list.
celenius
I see, but in the code above d is not a collection of primes. The list I included was what he was using to divide by (to test for primes). It wasn't my intention to fix his method, only show what his method would look like with proper syntax. Federico Ramponi's post has a valid algorithm.
xnine
+2  A: 

The following code is gross, but since 1000 is indeed a small index, it solves your problem in a fraction of a second (and it uses only the primitives you are supposed to know so far):

primesFound = 0
number = 1

while primesFound < 1000:
    number = number + 1          # start from 2

    # test for primality
    divisor = 2
    numberIsPrime = True
    while divisor*divisor <= number:   # while divisor <= sqrt(number)
        if number % divisor == 0:
            numberIsPrime = False
            break
        divisor = divisor + 1

    # found one?
    if numberIsPrime:
        primesFound = primesFound + 1

print number

You can test the solution here. Now you should find a more efficient solution, optimize and maybe go for the 1000000-th prime...

Federico Ramponi
also "divisor * divisor <= number" is equal to "divisor <= sqrt(number)" in cases where a teacher doesn't want you to use sqrt().
xnine
@xnine: yes! thank you, this is a good speed-up.
Federico Ramponi
(and rewriting the very same program in C with that speed-up, it takes ~10 seconds to get to the 1000000-th prime)
Federico Ramponi
correct me if I'm wrong but using this algorithm you should start at number 5 and increment 2 instead of 1. Now you are testing all even numbers which there is no point in testing.
Mazen Harake
@xnine: Good trick to avoid sqrt().. Also Mazen is right. No need to test even numbers.
Niraj Nawanit
@Federico: Wrong, it will only slow down. You need to compute sqrt only once for each number to be tested. But if you want to use (divisor * divisor), you will have to do this for each division..
Niraj Nawanit
@Niraj: I was referring to the previous (lame) version, where the test was divisor <= number/2; the algorithm was therefore O(n^2) and very slow (but it worked fine for the requested N=1000). Note that here we cannot use sqrt() "by requirement" ("We also don't yet know about importing libraries").
Federico Ramponi
A: 

To answer your subsequent question, 'How do I keep track of all the prime numbers?

One way of doing this is to make a list.

primeList = [] # initializes a list

Then, each time you test a number for whether it is prime or not, add that number to primeList

You can do this by using the 'append' function.

primeList.append( potprime )  # adds each prime number to that list

Then you will see the list filling up with numbers so after the first three primes it looks like this:

>>> primeList
[11, 13, 17]
celenius
A: 

Looks like I am late

It is quite straight forward that if a number is not divisible by any prime number, then that number is itself a prime number. You can use this fact to minimize number of divisions.

For that you need to maintain a list of prime numbers. And for each number only try to divide with prime numbers already in the list. To optimize further it you can discard all prime numbers more than square root of the number to be tested. You will need to import sqrt() function for that.

For example, if you test on 1001, try to test with 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 93 and 97. That should be enough. Also never try to find out if an even number is prime. So basically if you test an odd number n, then after that test next number: (n + 2)

Have tested the below code. The 1000th prime number is 7919. Not a big number!!

Code may be like:

from math import sqrt

primeList = [2]
num = 3
isPrime = 1

while len(primeList) < 1000:
    sqrtNum = sqrt(num)

    # test by dividing with only prime numbers
    for primeNumber in primeList:

        # skip testing with prime numbers greater than square root of number
        if num % primeNumber == 0:
            isPrime = 0
            break
        if primeNumber > sqrtNum:
            break

    if isPrime == 1:
        primeList.append(num)
    else:
        isPrime = 1

    #skip even numbers
    num += 2

# print 1000th prime number
print primeList[999]
Niraj Nawanit
checked at http://primes.utm.edu/lists/small/1000.txt .. 7919 is indeed 1000th prime
Niraj Nawanit