Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Project Euler Problem 3 Python help
New Posts  All Forums:Forum Nav:

Project Euler Problem 3 Python help

post #1 of 3
Thread Starter 
Can someone help me with this Python thing? Just learned python today, decided to do some programming on Project Euler with it. Can someone look over this script and see what is wrong with it?

Trying to find the largest palindrome by multiplying two 3 digit numbers:

Code:
def isPalindrome (n):
    """
    Determines if the object is a palindrome.
    """
    forward = str(n)
    backward = forward[::-1]
    if backward == forward:
        return True
    else:
        return False


if isPalindrome(10001) ==True:
    print ("true")

n1 = 101
n2 = 100
large = 100100
while n1 <1000:
    while n2 < n1:
        if isPalindrome(n1*n2):
            if (n1*n2 > large):
                large = n1*n2

    n2+=1
n1+=1

print (large)
Project Chloe
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5-2500k 4.4GHz 1.280v Fatal1ty Z68 Professional Gen3 MSI R6950 Twin Frozr III Corsair Vengence 8GB 1600MHz 
Hard DriveHard DriveOptical DriveOS
OCZ Vertex 4 Samsung Spinpoint F3 Asus 24X DVD Burner Windows 7 Ultimate 64bit 
MonitorMonitorKeyboardPower
ASUS VE228H ASUS VE228H Razor Blackwidow  Corsair 850HX 
CaseMouseMouse PadAudio
HAF X Logitech G500 Razer Goliathus Logitech G930 
  hide details  
Reply
Project Chloe
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5-2500k 4.4GHz 1.280v Fatal1ty Z68 Professional Gen3 MSI R6950 Twin Frozr III Corsair Vengence 8GB 1600MHz 
Hard DriveHard DriveOptical DriveOS
OCZ Vertex 4 Samsung Spinpoint F3 Asus 24X DVD Burner Windows 7 Ultimate 64bit 
MonitorMonitorKeyboardPower
ASUS VE228H ASUS VE228H Razor Blackwidow  Corsair 850HX 
CaseMouseMouse PadAudio
HAF X Logitech G500 Razer Goliathus Logitech G930 
  hide details  
Reply
post #2 of 3
Code:
        n2+=1
    n2 = 100
    n1+=1
Python is a real stickler about syntax. The increments have to be indented one more tab each, so they're done inside the loop. You also need to reset n2 at the end of each inner loop.

The answer is 906609.
Edited by C-bro - 8/12/11 at 7:45pm
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #3 of 3
Code:
    if backward == forward:
        return True
    else:
        return False
lol.. always a good one

Code:
return backward == forward


Code:
-if isPalindrome(10001) ==True:
+if isPalindrome(10001):
you did that further down... keep your expressions to the minimum, or at least try to be consistent all the way through.

I'm not sure what the python interpreter does, but chances are it probably does NOT do common subexpression elimination since it's a line by line interpreter as opposed to a compiler. You're using cpu cycles to calculate n1*n2 three times, calc it once and save it in a variable. But again if the interpreter does do that it won't make a difference .. as a general rule I try to factor out duplicate expressions and inline them when only used ONCE
Edited by lloyd mcclendon - 8/17/11 at 11:19pm
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Project Euler Problem 3 Python help