Two Plus Two Poker Forums

Two Plus Two Poker Forums (https://forumserver.twoplustwo.com/)
-   Computer and Technical Help (https://forumserver.twoplustwo.com/48/computer-technical-help/)
-   -   ** Python Support Thread ** (https://forumserver.twoplustwo.com/48/computer-technical-help/python-support-thread-1007515/)

Gazillion 10-11-2011 11:53 AM

Re: ** Python Support Thread **
 
This woud seem more elegant. I had to use a while loop rather than a for loop, because I kept getting errors that the number was too big when I called isfact with the proper argument.

Spoiler:

Code:

#The prime factors of 13195 are 5, 7, 13 and 29.
#What is the largest prime factor of the number 600851475143 ?

def isprime(val):
        if val == 1:
                return False
        for x in range(2, val):
                if val % x == 0:
                        return False
        else:
                return True

def isfact(arg):
        loop = arg/2+1               
        while  loop > 2:
                if arg % loop == 0:
                        if isprime(loop):
                                print loop
                                break
                loop -= 1

isfact(600851475143)



Still taking forever to brute force that number. 30 mins and counting.....

Gazillion 10-11-2011 06:20 PM

Re: ** Python Support Thread **
 
Uh oh - 6.5 hours later and my code is still running. The last time the code found a prime factor was probably over 4 hours ago, and while it's up into 10 digit numbers now I'd be surprised if it was legitimately taking this long to compute. I'm starting to suspect I may have hit an infinite loop. Could anybody tell from looking at the code in my previous post (or the one before it which prints every prime factor it encounters) where I am likely to have run into problems?

RoundTower 10-11-2011 07:24 PM

Re: ** Python Support Thread **
 
edit: disregard what this post used to say. I think your code is correct, but it will take a long time. Definitely you should start your prime-finding mission from the bottom instead of the top.

Spoiler:
the largest prime factor has less than ten digits, so you just haven't got there yet

Gazillion 10-11-2011 07:35 PM

Re: ** Python Support Thread **
 
I just came back to post that I think my code sucks balls. I got really exasperated after 7hrs and checked Wolfram for the answer. It turns out I got the correct answer a few seconds into my search, but then for some reason it kept on iterating and generating larger numbers which turned out not to be primes. Back to the drawing board for me, after I read up a little on prime sieves. My math let me down pretty hard here!

Roundtower, re: your spoiler

I had 2 algos running - one that started from 1, and one that started from the end. The second never produced anything, the first got the answer but then kept on going (code is in post 250)!

RoundTower 10-11-2011 07:36 PM

Re: ** Python Support Thread **
 
ok. a good way to check if you have got the answer would be to multiply all of the primes found already, and see if you have the answer.

Gazillion 10-11-2011 07:41 PM

Re: ** Python Support Thread **
 
Wow, thanks. I had no idea you could check that way. I really should have paid more attention in Math class.....

Gazillion 10-11-2011 07:46 PM

Re: ** Python Support Thread **
 
This code gets the answers immediately, which is what I started my 2nd attempt off with, but then it keeps on running, and running, so I scrapped it thinking it was faulty. It works (in that it instantly returns the prime factors), I just don't understand why it doesn't stop.

Code:


#The prime factors of 13195 are 5, 7, 13 and 29.
#What is the largest prime factor of the number 600851475143 ?

def isprime(val):
        if val == 1:
                return False
        for x in range(2, val):
                if val % x == 0:
                        return False
        else:
                return True

def isfact(arg):
        loop = 1
               
        while  loop < arg/2+1:
                if arg % loop == 0:
                        if isprime(loop):
                                print loop
                loop += 1
       


isfact(600851475143)


I think I've found out what I was doing wrong - I just don't quite understand why yet. The problem was in the way I set up the while loop in isfact(). As sooon as I re-write that function the way I've set out below, the program runs and completes in < 1 sec:

Code:

def isfact(arg):
        loop = 1
        import math
               
        while  loop < math.sqrt(arg):
                if arg % loop == 0:
                        if isprime(loop):
                                print loop
                loop += 1

Edit: nope, I still suck. The code works fine for the number in question, but totally falls over if I feed isfact() with any other value.

FluxCapacitor 10-11-2011 10:07 PM

Re: ** Python Support Thread **
 
Quote:

Edit: nope, I still suck. The code works fine for the number in question, but totally falls over if I feed isfact() with any other value.
What do you mean? Your code works for me and is very similar to mine.

The only change I would make is to get rid of the loop counter:

Code:

def isfact(arg):
    for loop in range(2,int(arg**0.5)+1):
        if arg % loop == 0:
            if isprime(loop) == True:
                print loop,'is prime'

That makes it a bit more elegant IMO.

Neko 10-11-2011 10:29 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by FluxCapacitor (Post 29229386)
What do you mean? Your code works for me and is very similar to mine.

The only change I would make is to get rid of the loop counter:

Code:

def isfact(arg):
    for loop in range(2,int(arg**0.5)+1):
        if arg % loop == 0:
            if isprime(loop) == True:
                print loop,'is prime'

That makes it a bit more elegant IMO.

Also, just a note on style, it's kinda bad form to do comparisons with True & False.

Instead of :

Code:

if my_test(x) == True:
    print "truth"
if another_test(x) == False:
    print "false"

say:


Code:

if my_test(x):
    print "truth"
if not another_test(x):
    print "false"

There's also no reason you can't combine those two if statements:

Code:

def isfact(arg):
    for loop in range(2,int(arg**0.5)+1):
        if arg % loop == 0 and isprime(loop):
            print loop,'is prime'


FluxCapacitor 10-11-2011 10:36 PM

Re: ** Python Support Thread **
 
Hmm. Cool thanks for the true/false tip. Why is that bad form? (I'm only an amateur programmer obv)

Regarding the two if statements: If I combine them into one statement won't isprime(loop) execute for every 'loop' in range(...)? My thinking was that I could save time by nesting the two statements.

But, I just checked and for this example there is no noticeable time difference.

sorrow 10-11-2011 10:39 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Gazillion (Post 29226141)
Wow, thanks. I had no idea you could check that way. I really should have paid more attention in Math class.....

You can actually do this problem without doing an isprime() test, something i didn't realise until I got to a later problem. All numbers can be expressed in ony prime factors. This is exploitable.

FYI there is a Project Euler thread in this forum

Neko 10-11-2011 10:53 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by FluxCapacitor (Post 29229852)
Hmm. Cool thanks for the true/false tip. Why is that bad form? (I'm only an amateur programmer obv)

Regarding the two if statements: If I combine them into one statement won't isprime(loop) execute for every 'loop' in range(...)? My thinking was that I could save time by nesting the two statements.

But, I just checked and for this example there is no noticeable time difference.

Good thought and it may be true in some languages but Python does short circuit evaluation so isprime(loop) will only be executed if the first statement evaluates to True. If the first part evaluates to False then the evaluation of the second part can be skipped since "False and other_expression" can never be true.

This also works in similar manner for or statements. In the following block:

Code:

for x in range(10):
    if True or some_func(x):
        print x

some_func(x) will never be called since the if statement will always be true regardless of what some_func returns.

Here's an example that makes it a bit more clear:

Code:


def test(x):
    print x
    return True

for x in range(3):
    if True or test(x):
        print "true and test never called"
       
for x in range(3):
    if False or test(x):
        print "true because test returns true"

and it's output:

Code:

true and test never called
true and test never called
true and test never called
0
true because test returns true
1
true because test returns true
2
true because test returns true


FluxCapacitor 10-12-2011 12:13 AM

Re: ** Python Support Thread **
 
Cool. Thanks for the explanation.

kerowo 10-21-2011 11:59 PM

Re: ** Python Support Thread **
 
We have directories that end up with thousands of files in them, so many that ls times out. This makes it very difficult to look for a particular file in the directory and I'm going to write a little ditty to clean that up. What I'd like to avoid doing is writing something that causes more harm than good, so instead of just getting a glob that grabs all of the files in the directory and tries to work with them one after the other I'd rather limit the number of files I slurp and run the process multiple times (or run multiple slurps). Will glob.iglob(path) do that?

Xhad 10-26-2011 08:44 PM

Re: ** Python Support Thread **
 
Re: the Windows IDE talk some pages back, I just discovered this:

http://****************/projects/pywin32/

Neko 10-26-2011 09:05 PM

Re: ** Python Support Thread **
 
Pywin32 is great. I'm using it pretty extensively at work to read data from Excel sheets and automate a whole bunch of stuff that would have been a huge PITA using VBA.

tyler_cracker 10-27-2011 11:18 AM

Re: ** Python Support Thread **
 
we use pywin32 at work to manage Windows services.

_dave_ 10-27-2011 06:19 PM

Re: ** Python Support Thread **
 
not much info on that page suggesting what it does? presumably python bindings for the entire Win32 API? but also COM (from the excel use above)?

Madmax2000 10-27-2011 10:26 PM

Re: ** Python Support Thread **
 
I am learning python but I'm using 2.5.4 because the literature i have on it is old. Here is my question: Where is my syntax error stemming from it looks like it wants me to correct the 5 in 2.5.4, it doesn't point to any other errors. Here is my code, and tell me if it works or not because there's no teacher to grade me. The program is meant to ask you your last name then ask you your first, then print them out in first name to last name in that order.

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

************************************************** **************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
************************************************** **************

IDLE 1.2.4
>>> question1 = "What is your last name?"
>>> print question1
What is your last name?
>>> answer1 = raw_input()

>>> question2 = "What is your first name?"
>>> print question2
What is your first name?
>>> answer2 = raw_input()

>>> print answer2

>>> print answer1

>>>

Gazillion 10-28-2011 12:17 AM

Re: ** Python Support Thread **
 
I'm a programming noob too, but syntactically it seems fine to me. The one thing I would say is that you can probably halve your variables by forgoing the assignment of the "question" variables, and doing away with printing the prompts like this:

Code:

lastName = raw_input("What is your last name? :")
firstName = raw_input("What is your first name? :")
print firstName, lastName


tyler_cracker 10-28-2011 12:28 AM

Re: ** Python Support Thread **
 
i don't see a syntax error in your post?

Madmax2000 10-28-2011 12:46 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Gazillion (Post 29516545)
I'm a programming noob too, but syntactically it seems fine to me. The one thing I would say is that you can probably halve your variables by forgoing the assignment of the "question" variables, and doing away with printing the prompts like this:

Code:

lastName = raw_input("What is your last name? :")
firstName = raw_input("What is your first name? :")
print firstName, lastName


I tried your coding and I get the same syntax error. I know it has nothing to do with the coding because when I check the module in IDLE, I get the error message, and then it highlights the "5" in "python 2.5.4 at the very top of the screen, indicating that that is where the error is. So maybe I have to download another version of python?

jjshabado 10-28-2011 07:49 AM

Re: ** Python Support Thread **
 
I have a wild guess here that you're pasting that whole block of text into the command line (or just trying to run that whole block of code) so the first word "python" is opening up the interpreter and the next word 2.5.4 gives you a syntax error.

Can you give more details on what you're doing to run this?

Madmax2000 10-28-2011 10:26 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by jjshabado (Post 29520637)
I have a wild guess here that you're pasting that whole block of text into the command line (or just trying to run that whole block of code) so the first word "python" is opening up the interpreter and the next word 2.5.4 gives you a syntax error.

Can you give more details on what you're doing to run this?

Here's what I see when I open up python, I hope the image is not too small.

http://i1090.photobucket.com/albums/...onprntscrn.jpg

So "python 2.5.4 etc..." is already generated when I open up python. So at the first command line is where I start my code or the other posters code and i get the same syntax error, and the only thing that gets highlighted when I try to run it is the "5".

TheIrishThug 10-28-2011 10:36 AM

Re: ** Python Support Thread **
 
Can we see the code you ran and the actual error message? This will make it easier for us to see what is actually going wrong and why it is happening.


All times are GMT -4. The time now is 05:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.

Copyright © 2008-2020, Two Plus Two Interactive