Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: April 25, 2024, 3:54 pm

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project Euler
#21
RE: Project Euler
(July 3, 2016 at 8:16 am)SteelCurtain Wrote: Ahh, I see what you mean.

You want to just initialize variables.

I think Python only allows you to do that with lists and tuples.

No, you can initialize variables with Python. What you did inside the procedure/function in Python code (a = 5) was an example of initializing, right?

Anyway, I can't remember why I wished variables in Python could just be declared/defined without initial assignment, but I did just learn something new in Python. Apparently, you can test for whether or not a variable has been defined using globals() or locals().

For example:
Code:
v = 0

if 'v' not in globals():
    print 'v not defined'
else:
    print 'v defined'

It won't throw an error at you for not defining v if you didn't. But note that you have to wrap quotes around the 'v' for this to run without errors.
Reply
#22
RE: Project Euler
Well, after my code for #12 ran for about 3 hours on my monster machine, I got the right answer... Tongue

Not exactly efficient, but it worked... :S
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great

PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
Reply
#23
RE: Project Euler
(July 3, 2016 at 10:24 pm)SteelCurtain Wrote: Well, after my code for #12 ran for about 3 hours on my monster machine, I got the right answer... Tongue

Not exactly efficient, but it worked... :S

If this is still on an online IDE, this must be a good one. 3 hours is very impressive.

Since you solved the triangle number problem, did you just count all the factors less than sqrt(n), where n represents each triangle number checked? And multipled by two for non-perfect squares, and additionally added a 1 for perfect squares?
Reply
#24
RE: Project Euler
I'm now using IDLE, the Python IDE that comes packaged with Python. My code was rather simple, I just checked the number of divisors for each triangle number until one had more than 500 divisors. I think it was like 8 lines of code w/o the function def.

The next few were easy, I'm up to number 17. This one isn't hard, but writing the code is time consuming.
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great

PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
Reply
#25
RE: Project Euler
Problem #17 was easy in theory, required a lot of debugging...

Code:
def main():
    tot = 0

    for i in range(1, 1001):
        n = str(i)
        
        if len(n) == 1: #one digit
            tot += ones('0', n[0])
        elif len(n) == 2: #two digits
            tot += tens(n[0], n[1]) + ones(n[0], n[1])
        elif len(str(i)) == 3: #three digits
            tot += hundreds(n[0], n[1], n[2]) + tens(n[1], n[2]) + ones(n[1], n[2])

        else: #add letters for 'one thousand'
            tot += 11

    print(tot)


def ones(tens, ones):
    if tens == '1':
        return 0
    else:
        if any(ones == x for x in ['1', '2', '6']):
            return 3
        elif any(ones == x for x in ['4', '5', '9']):
            return 4
        elif any(ones == x for x in ['3', '7', '8']):
            return 5
        else:
            return 0

def tens(tens, ones):
    if tens == '1':
        if any(ones == x for x in ['1', '2']):
            return 6
        elif any(ones == x for x in ['5', '6']):
            return 7
        elif any(ones == x for x in ['3', '4', '8', '9']):
            return 8
        elif ones == '7':
            return 9
        elif ones == '0':
            return 3
    elif any(tens == x for x in ['2', '3', '8', '9']):
        return 6
    elif any(tens == x for x in ['4', '5', '6']):
        return 5
    elif tens == '7':
        return 7
    else:
        return 0
    
def hundreds(hunds, tns, ons):
    if (tns == '0' and ons == '0'):
        return 7 + ones('0', hunds)
    else:
        return 10 + ones('0', hunds)
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great

PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
Reply
#26
RE: Project Euler
(July 4, 2016 at 4:47 am)SteelCurtain Wrote: Problem #17 was easy in theory, required a lot of debugging...

Code:
def main():
   tot = 0

   for i in range(1, 1001):
       n = str(i)
       
       if len(n) == 1: #one digit
           tot += ones('0', n[0])
       elif len(n) == 2: #two digits
           tot += tens(n[0], n[1]) + ones(n[0], n[1])
       elif len(str(i)) == 3: #three digits
           tot += hundreds(n[0], n[1], n[2]) + tens(n[1], n[2]) + ones(n[1], n[2])

       else: #add letters for 'one thousand'
           tot += 11

   print(tot)


def ones(tens, ones):
   if tens == '1':
       return 0
   else:
       if any(ones == x for x in ['1', '2', '6']):
           return 3
       elif any(ones == x for x in ['4', '5', '9']):
           return 4
       elif any(ones == x for x in ['3', '7', '8']):
           return 5
       else:
           return 0

def tens(tens, ones):
   if tens == '1':
       if any(ones == x for x in ['1', '2']):
           return 6
       elif any(ones == x for x in ['5', '6']):
           return 7
       elif any(ones == x for x in ['3', '4', '8', '9']):
           return 8
       elif ones == '7':
           return 9
       elif ones == '0':
           return 3
   elif any(tens == x for x in ['2', '3', '8', '9']):
       return 6
   elif any(tens == x for x in ['4', '5', '6']):
       return 5
   elif tens == '7':
       return 7
   else:
       return 0
   
def hundreds(hunds, tns, ons):
   if (tns == '0' and ons == '0'):
       return 7 + ones('0', hunds)
   else:
       return 10 + ones('0', hunds)

If you want to see my code, go to Page 8 in the corresponding thread and it should be the 15th post. Country is Australia, I'm right after the Chinese dude.
Reply
#27
RE: Project Euler
Nice. It looks like you built a string for each number, then counted the letters? I like that approach.
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great

PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
Reply
#28
RE: Project Euler
(July 4, 2016 at 5:31 am)SteelCurtain Wrote: Nice. It looks like you built a string for each number, then counted the letters? I like that approach.

Yep, and two lists for digits, teen numbers, and multiples of ten.

A bit long, but I never cared about longevity, just as long as the code runs well and doesn't crash.
Reply
#29
RE: Project Euler
This nerd has just reached problem 41.
Reply



Possibly Related Threads...
Thread Author Replies Views Last Post
  [Euler] Need clarification shadowdancer 11 3668 November 22, 2011 at 2:17 am
Last Post: shadowdancer



Users browsing this thread: 1 Guest(s)