Computers are amazing
Post Reply
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
20-02-2012, 11:10 AM (This post was last modified: 20-02-2012 11:14 AM by Malleus.)
RE: Computers are amazing
@Rook2004
The for in C has a completely different syntax from Python. Something like:

Quote:for(n=0; n < 1000; n++)

Leela didn't fully figure out for in Python and obviously I don't know it either so we used while thanks to the roughly similar syntax.
Also, keep in mind that Leela is noobish both in C and in Python. She can't write a function that returns values yet. We had to work with functions that get taught to total beginners at second lesson ever (or so).

The code you wrote prints all prime numbers from 1 to 1000. The goal of the problem was to get the 1000th prime number (which is somewhere over 7000). The code we wrote doesn't exactly do that but it displays the first 1000 prime numbers because at some point we were troubleshooting and we moved the "print x" to show us all the results rather than the last one. We opened the beer when we finally got it and were too lazy to change it after that Big Grin Leela will present her solution when she gets home from work, I guess.

Oh, no Hallucinations 4:11 says the 'gilded sheep should be stewed in rat blood' but Morons 5:16 contradicts it. (Chas)

I would never shake a baby unless the recipe requires it.
Find all posts by this user
Like Post Quote this message in a reply
20-02-2012, 01:38 PM
RE: Computers are amazing
Here our solution:
PHP Code:
3                 

counter 
2

2                 #stopper for the while

fail 0



while (counter 1001):        

    
2                                

    fail 
0

    
while (x):

        if 
== 0:

            
fail 1

        n 
1

    x 
1

    
if fail == 0:

        
counter counter 1

        
print 1

            

## should be 7919 

[Image: 69p7qx.gif]
Visit this user's website Find all posts by this user
Like Post Quote this message in a reply
20-02-2012, 03:06 PM
RE: Computers are amazing
(20-02-2012 11:10 AM)Malleus Wrote:  @Rook2004
The for in C has a completely different syntax from Python. Something like:

Quote:for(n=0; n < 1000; n++)

Leela didn't fully figure out for in Python and obviously I don't know it either so we used while thanks to the roughly similar syntax.
Also, keep in mind that Leela is noobish both in C and in Python. She can't write a function that returns values yet. We had to work with functions that get taught to total beginners at second lesson ever (or so).

The code you wrote prints all prime numbers from 1 to 1000. The goal of the problem was to get the 1000th prime number (which is somewhere over 7000). The code we wrote doesn't exactly do that but it displays the first 1000 prime numbers because at some point we were troubleshooting and we moved the "print x" to show us all the results rather than the last one. We opened the beer when we finally got it and were too lazy to change it after that Big Grin Leela will present her solution when she gets home from work, I guess.

Ah-hah, I didn't actually bother to read the problem statement at 5am. Smile Replace my for loop with while len(primes) < 1000: and it should be good to go.

Yeah, for loops in C are basically syntactic sugar for a while loop plus a variable declaration plus an operation done each iteration. In Python, 'for' is tied to list iteration and is basically what other languages call "foreach". It was a weird leap for me when I first learned Python.

In my solution, I also cheated by using an array to store primes I already found. So to check if n is prime, rather than having to look at every number between 2 and n, I only had to look at all the primes in the array.

This problem is great because if you start looking for bigger and bigger primes our solutions will start to slow down substantially because we're using trial division to check whether a number is prime. With more effort and programming experience, you can come back and try implementing other tests for primality.

I mean...if you're a huge nerd like me...who gets excited when people learn to program...

And frankly, prime numbers are kind of a big deal.
Find all posts by this user
Like Post Quote this message in a reply
[+] 1 user Likes rook2004's post
21-02-2012, 08:05 AM
RE: Computers are amazing
Well, I'm not sure how things work in Python, but I would be able to write a C function that skips the rest of the cycle as soon as it reaches the first perfect division and moves on to the next number. That also increases speed considerably. That and only dividing by maximum half the number (no number can be evenly divided by something larger than its half, no point making the program try it out) Your "cheat" is a very good idea and combined with mine we would get a pretty optimized prime number selector as far as this method can take us.

However Leela wasn't sure whether something like "break" exists in Python and I didn't want to confuse her by pushing functions ahead of her class. She also wasn't sure how python integers deal with fractional results so, again I decided not to push it.

Oh, no Hallucinations 4:11 says the 'gilded sheep should be stewed in rat blood' but Morons 5:16 contradicts it. (Chas)

I would never shake a baby unless the recipe requires it.
Find all posts by this user
Like Post Quote this message in a reply
21-02-2012, 04:04 PM
RE: Computers are amazing
(21-02-2012 08:05 AM)Malleus Wrote:  Well, I'm not sure how things work in Python, but I would be able to write a C function that skips the rest of the cycle as soon as it reaches the first perfect division and moves on to the next number. That also increases speed considerably. That and only dividing by maximum half the number (no number can be evenly divided by something larger than its half, no point making the program try it out) Your "cheat" is a very good idea and combined with mine we would get a pretty optimized prime number selector as far as this method can take us.

However Leela wasn't sure whether something like "break" exists in Python and I didn't want to confuse her by pushing functions ahead of her class. She also wasn't sure how python integers deal with fractional results so, again I decided not to push it.

When I work with these things every day, I forget how complicated they are. Sad

Also, as Wikipedia told me, you don't even have to go up to half; you can stop at the square root since any number bigger than sqrt(x) would have to be multiplied by a number smaller than sqrt(x) to get x.

I love functions. Leela, when you get to functions, you will love them, too. ;D

Especially RECURSIVE functions.
Find all posts by this user
Like Post Quote this message in a reply
[+] 2 users Like rook2004's post
21-02-2012, 06:22 PM
RE: Computers are amazing
(21-02-2012 04:04 PM)rook2004 Wrote:  
(21-02-2012 08:05 AM)Malleus Wrote:  Well, I'm not sure how things work in Python, but I would be able to write a C function that skips the rest of the cycle as soon as it reaches the first perfect division and moves on to the next number. That also increases speed considerably. That and only dividing by maximum half the number (no number can be evenly divided by something larger than its half, no point making the program try it out) Your "cheat" is a very good idea and combined with mine we would get a pretty optimized prime number selector as far as this method can take us.

However Leela wasn't sure whether something like "break" exists in Python and I didn't want to confuse her by pushing functions ahead of her class. She also wasn't sure how python integers deal with fractional results so, again I decided not to push it.

When I work with these things every day, I forget how complicated they are. Sad

Also, as Wikipedia told me, you don't even have to go up to half; you can stop at the square root since any number bigger than sqrt(x) would have to be multiplied by a number smaller than sqrt(x) to get x.

I love functions. Leela, when you get to functions, you will love them, too. ;D

Especially RECURSIVE functions.

I love recursive functions. Tongue

That's how the sudoku solver I posted works. I also wrote a cool little calculator thing that can evaluate math expressions like (3+1)*(5*(3+1))^2 using a recursive function.
Find all posts by this user
Like Post Quote this message in a reply
21-02-2012, 07:18 PM
RE: Computers are amazing
(21-02-2012 06:22 PM)Ben Wrote:  I love recursive functions. Tongue

That's how the sudoku solver I posted works. I also wrote a cool little calculator thing that can evaluate math expressions like (3+1)*(5*(3+1))^2 using a recursive function.

But does it have the correct order of operations for ^? Smile
Find all posts by this user
Like Post Quote this message in a reply
21-02-2012, 08:07 PM (This post was last modified: 28-04-2012 08:41 AM by lucradis.)
RE: Computers are amazing
(21-02-2012 07:18 PM)rook2004 Wrote:  
(21-02-2012 06:22 PM)Ben Wrote:  I love recursive functions. Tongue

That's how the sudoku solver I posted works. I also wrote a cool little calculator thing that can evaluate math expressions like (3+1)*(5*(3+1))^2 using a recursive function.

But does it have the correct order of operations for ^? Smile

Yup, it does orders of operation.

Edited By Admin By Request.

(I tried to just upload the .py but the forum says that extension isn't allowed. :dodgySmile
Find all posts by this user
Like Post Quote this message in a reply
22-02-2012, 06:35 PM
RE: Computers are amazing
(21-02-2012 08:07 PM)Ben Wrote:  
(21-02-2012 07:18 PM)rook2004 Wrote:  But does it have the correct order of operations for ^? Smile

Yup, it does orders of operation.

No, I meant is the exponentiation order of ops correct. It isn't. Try 2^2^3. You get the same answer as Excel, 64. The right answer is 256, as Wolfram Alpha can tell you.

It's a common gotcha, which is why I asked. Smile
Find all posts by this user
Like Post Quote this message in a reply
22-02-2012, 07:58 PM (This post was last modified: 28-04-2012 04:48 PM by lucradis.)
RE: Computers are amazing
(22-02-2012 06:35 PM)rook2004 Wrote:  
(21-02-2012 08:07 PM)Ben Wrote:  
(21-02-2012 07:18 PM)rook2004 Wrote:  But does it have the correct order of operations for ^? Smile

Yup, it does orders of operation.

No, I meant is the exponentiation order of ops correct. It isn't. Try 2^2^3. You get the same answer as Excel, 64. The right answer is 256, as Wolfram Alpha can tell you.

It's a common gotcha, which is why I asked. Smile

Ah, I didn't think of that. Big Grin

Trying to think of a fix.... Maybe for the exponent operator search the expression backwards?

-EDIT-

This seems to work better:

Edited By Admin By Request.
Find all posts by this user
Like Post Quote this message in a reply
Post Reply
Forum Jump: