November 15, 2024, 12:47:41 AM

1,531,348 Posts in 46,734 Topics by 1,523 Members
› View the most recent posts on the forum.


I decided to step up to Guff's challenge

Started by ncba93ivyase, January 19, 2009, 11:18:13 PM

previous topic - next topic

0 Members and 2 Guests are viewing this topic.

Go Down

ncba93ivyase

A solution to 25:
Code Select
x = []
y, z = 0, 2
while len(x) < 2:
x.append(1)
y += 1
while len(x) < 10:
x.append((x[y-2])+(x[y-1]))
y+=1
z+=1
if len(x) > 5:
del x[0]
y -= 1
if len(str(x[4])) > 999:
break
print z

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

ncba93ivyase

Last I checked, JMV only had 9 problems solved and I now have 10.
Code Select
x = []
y, z, i = 0, 2, 0
while len(x) < 2:
x.append(1)
y += 1
while len(x) < 6:
x.append((x[y-2])+(x[y-1]))
y+=1
z+=1
if x[y-1] %2 == 0: i+= x[y-1]
if len(x) > 5:
del x[0]
y -= 1
if x[4] > 4000000:
break
print i
And there's my hideous solution to 2.

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

hey those there fibonatchis numbers sure do show up a few times i reckon a feller such as yerself could write a function for them instead of resorting to while loops every cotton-pickin' time bassir;

also my solution for 24 is two lines long and takes ~0.06 seconds to run  n_u
granted it can be solved by hand if you do the math but brute force is usually more fun

Daddy

Quote from: Pancake Persona on February 18, 2009, 10:22:11 PM
Last I checked, JMV only had 9 problems solved and I now have 10.
Code Select
x = []
y, z, i = 0, 2, 0
while len(x) < 2:
x.append(1)
y += 1
while len(x) < 6:
x.append((x[y-2])+(x[y-1]))
y+=1
z+=1
if x[y-1] %2 == 0: i+= x[y-1]
if len(x) > 5:
del x[0]
y -= 1
if x[4] > 4000000:
break
print i
And there's my hideous solution to 2.

Code Select
#!/usr/bin/python
# Filename : Euler_002.py
#Find the sum of all the even-valued terms in the sequence which do not exceed four million.
a,d,b = 0,0,1
while a <= 4000000:
c=a
a=b
b+=c
if a%2==0:
  d+= a
print d


guff

yeah i've got no idea what the hell lawlz is doing with his code

also god damn start using python3 already all of you  akudood;

ncba93ivyase

Quote from: guff on February 19, 2009, 07:09:18 AM
yeah i've got no idea what the hell lawlz is doing with his code

also god damn start using python3 already all of you  akudood;
i needed to create a list of fibonacci numbers for something and was like 'hey i can just recycle this'

although i see lots of problems with it and it really is terrible goonish

And pygame doesn't support python 3.0, so I can't.

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on February 19, 2009, 12:43:42 PM
i needed to create a list of fibonacci numbers for something and was like 'hey i can just recycle this'

although i see lots of problems with it and it really is terrible goonish

And pygame doesn't support python 3.0, so I can't.
uh yeah that's what modules and functions are for  baddood;

uh yeah it is and it's extremely complicated and i'm still not sure what the hell is going on in it and why are there so many magic numbers in your code  akudood;

well then pygame SUCKS akudood;

ncba93ivyase

Quote from: guff on February 19, 2009, 01:36:38 PM
uh yeah that's what modules and functions are for  baddood;

uh yeah it is and it's extremely complicated and i'm still not sure what the hell is going on in it and why are there so many magic numbers in your code  akudood;

well then pygame SUCKS akudood;
for some reason, the crazier my code is the more i like it

short "simple" stuff wracks my brain for some reason, and really crazy methods of doing everything come smoother to me for some reason.

but i can really say this part of my code was completely pointless and stupid because i could've just started the list i wanted with the other thing with [1, 1] instead of this slop:
Code Select
while len(x) < 2:
  x.append(1)
  y += 1


goonish

My main problem in both writing and programming is I never review my work and I just go with what works the first time. I'm certain if I spent 30 more seconds doing things I could do everything better, but it's an impossible habit to break.

also pygame doesn't suck, it's just that it's updated like once every four score and seven years

and it's better than learning sepples

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

ncba93ivyase

a very, very slow solution to 5
Code Select
i = 0
while True:
if i != 0 and i % 11 == 0 and i % 13 == 0 and i % 14 == 0 and i % 15 == 0 and i % 16 == 0 and i % 17 == 0 and i % 18 == 0 and i % 19 == 0:
print i
break
i += 20


I'm certain guff has a better way of doing it, because this takes 5.63 seconds to run :'(

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on February 20, 2009, 02:23:33 PM
I'm certain guff has a better way of doing it, because this takes 5.63 seconds to run :'(
uh well yeah look up euclid's algorithm
it's an easy peasy and pretty quick way to compute the gcd of two numbers, from which the lcm is easily to computed
then, to compute the gcd or lcm of a list, since both functions are associative, all you have to do is call reduce() with the function and that list (in this case range(1,21)) as arguments

and although i generally avoid recursion if you're too lazy to actually read the article the gcd function can be implemented as a one-liner:
Code Select
def gcd(a,b): return a if b==0 else gcd(b,a%b)
from there, you can write the lcm function as:
Code Select
def lcm(a,b): return a*b//gcd(a,b)
the rest is left as an exercise to the stupid, incompetent reader  akudood;

ncba93ivyase

Problem 55:
Code Select
import time
total = 0
for starter in range(0, 10000):
x = starter + int(str(starter)[::-1])
if x != int(str(x)[::-1]):
for i in range(1, 49):
x += int(str(x)[::-1])
if x == int(str(x)[::-1]):
break
if i == 48 and x != int(str(x)[::-1]):
total += 1
print "Answer is %d. Solved in %.3f" %(total, time.clock())

.18 seconds to solve, so not too bad.

Not even 6000 people solved it yet, making me feel even better about it.

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

i already had 55 solved so i decided to go back and try and make my solution as short and obfuscated as possible a la lawlz except i did it on purpose:
Code Select
lychrel = lambda n,i=1: False if (n+int(str(n)[::-1]))==int(str(n+int(str(n)[::-1]))[::-1]) else True if i >= 50 else lychrel(n+int(str(n)[::-1]), i+1)
print([lychrel(i) for i in range(10000)].count(True))

takes about 0.46 seconds  baddood;

the solution i'm keeping for my records though is far more verbose and actually makes use of function definitions
also i could've done this in one line if python supported anonymous recursion but i don't think it does but i did learn that if you bind a name to a lambda you can recurse akudood;

ncba93ivyase

Quote from: guff on February 21, 2009, 01:24:47 PM
takes about 0.46 seconds  baddood;

so how fast is your real solution or is mine better spam;

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on February 21, 2009, 01:29:05 PM
so how fast is your real solution or is mine better spam;
~0.26 (~0.36 if i use the recursive lyrchel function that's commented out instead which i think is prettier) so yours is slightly faster but god damnit i actually use functions so i've got overhead from that:
Code Select
def reverse_digits(n): return int(str(n)[::-1])

def is_palindrome(n): return n == reverse_digits(n)

#def lychrel(n, i=1):
# return False if is_palindrome(n + reverse_digits(n)) else True if i >= 50 else lychrel(n + reverse_digits(n), i+1)

def lychrel(n):
for i in range(1, 50):
n = n + reverse_digits(n)
if is_palindrome(n): return False
return True

if __name__ == '__main__':
from time import time

start = time()
print(len([True for i in range(10000) if lychrel(i)]), time() - start)


maps;

also open up an interpreter session and run:
Code Select
from test import pystone
pystone.full()

for comparison
my machine gets 49224.4 pystones per second

ncba93ivyase

Quote from: guff on February 21, 2009, 01:39:32 PM
~0.26 (~0.36 if i use the recursive lyrchel function that's commented out instead which i think is prettier) so yours is slightly faster but god damnit i actually use functions so i've got overhead from that:
Code Select
def reverse_digits(n): return int(str(n)[::-1])

def is_palindrome(n): return n == reverse_digits(n)

#def lychrel(n, i=1):
# return False if is_palindrome(n + reverse_digits(n)) else True if i >= 50 else lychrel(n + reverse_digits(n), i+1)

def lychrel(n):
for i in range(1, 50):
n = n + reverse_digits(n)
if is_palindrome(n): return False
return True

if __name__ == '__main__':
from time import time

start = time()
print(len([True for i in range(10000) if lychrel(i)]), time() - start)


maps;

also open up an interpreter session and run:
Code Select
from test import pystone
pystone.full()

for comparison
my machine gets 49224.4 pystones per second
>>> from test import pystone
>>> pystone.full()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'full'

maps;

also, functions don't really matter if i'm really only doing one little thing like this, and speed is more important

although i knew if i didn't make up some function to do something, you'd get grumpy but i was just like 'lol' spam;

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

Go Up