December 22, 2024, 11:54:24 AM

1,531,361 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 1 Guest are viewing this topic.

Go Down

guff

Quote from: Pancake Persona on February 21, 2009, 01:44:37 PM
>>> from test import pystone
>>> pystone.full()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'full'
oh duh i'm an idiot it's pystone.main() not full()
Quote from: Pancake Persona on February 21, 2009, 01:44:37 PM
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;
well it's just good practice  akudood;

ncba93ivyase

Quote from: guff on February 21, 2009, 01:47:46 PM
oh duh i'm an idiot it's pystone.main() not full()well it's just good practice  akudood;
54945.1 pystones/second

and i just ran your solution and got like .201 s

Good practice doesn't necessarily mean good solutions. spam;

although i guess that's not something i should talk about maps;

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

Daddy

>>> from test import pystone
>>> pystone.main()
Pystone(1.1) time for 50000 passes = 0.99
This machine benchmarks at 50505.1 pystones/second
>>>

AND AGAIN

>>> from test import pystone
>>> pystone.main()
Pystone(1.1) time for 50000 passes = 0.97
This machine benchmarks at 51546.4 pystones/second
>>>



Daddy

Code Select
#!/usr/bin/python
# Filename : Euler_055.py
#How many Lychrel numbers are there below ten-thousand?
numLy=0
def isPalindrome(n): return str(n) == str(n)[::-1]
def rev(n): return int(str(n)[::-1])
def lychrel(n):
for i in range(50):
  n+=rev(n)
  if isPalindrome(n):
   return False
return True
for i in range(10000):
if lychrel(i):
  numLy+=1
print "Number of lychrel numbers is %d."%(numLy)   

Daddy

Code Select
#!/usr/bin/python
# Filename : Euler_009.py
#There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
#a<b<c && a**2 + b**2 = c**2
from time import time
start = time()
def equalsC(a,b,c):
    if c**2 == (a**2 + b**2):
        return True
def sizeCheck(a,b,c):
    if a<b<c:
        return True
def sumCheck(a,b,c):
    if a+b+c==1000:
        return True 
for a in range(500):
    for b in range(500):
        c=1000-a-b
        if equalsC(a,b,c) and sizeCheck(a,b,c) and sumCheck(a,b,c):
            print a*b*c
            print time() - start
            exit()

Daddy

Code Select
#!/usr/bin/python
# Filename : Euler_008.py
#Find the greatest product of five consecutive digits in the 1000-digit number.
from time import time
start = time()
bigFuckingNumber="*number removed since it stretches the code box*"
prod = 0
for i in range(996):
dummy =int(bigFuckingNumber[i]) * int(bigFuckingNumber[i+1]) * int(bigFuckingNumber[i+2]) * int(bigFuckingNumber[i+3]) * int(bigFuckingNumber[i+4])
if dummy> prod:prod = dummy
print prod
print time() - start

Daddy

February 23, 2009, 10:13:36 PM #111 Last Edit: February 23, 2009, 10:20:13 PM by Raekewn
Code Select
#!/usr/bin/python
# Filename : Euler_013.py
# Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
file = open('files/13.txt')
lineList = []
for line in file:lineList.append(int(line))
print str(sum(lineList))[:10]

guff


guff

solved problem 231, and my code runs in about 56 seconds  akudood;
Code Select
from euler_functions import sieve_of_erat

def factor_sum(factors_dict):
return sum(factor * exponent for factor, exponent in factors_dict.items())

def dict_add(dict_a, dict_b):
result_dict = {}
for key in set(dict_a.keys()) | set(dict_b.keys()):
result_dict[key] = dict_a.get(key, 0) + dict_b.get(key, 0)
return result_dict

def binomial_factors(n, k): # n!/(k!*(n-k)!) factorization = n! factors - k! factors - (n-k)! factors
primes = {n: sieve_of_erat(n), k: sieve_of_erat(k), n-k: sieve_of_erat(n-k)}
factors = {n: {}, k: {}, n-k: {}}

for number in (n, k, n - k):
for p in primes[number]:
p_count = 0
i = 1
while number // (p ** i) >= 1:
if number == n:
p_count += number // (p ** i)
else:
p_count -= number // (p ** i)
i += 1
factors[number][p] = p_count
return dict_add(factors[n], dict_add(factors[k], factors[n-k]))

doesn't include the code that actually generates the answer though just so no one gets tempted  baddood;

after looking at the forum, there's many different ways of doing it some of which are faster and some of which are shorter but this is just what i came up with first baddood;

Daddy

GUFF GET ONLINE HELP ;_;

I did 14 and it runs in about 13 seconds(down from 49) if I check only odd numbers and start at 500,001(sicnce all of the odds will return an even which then gets cut in half, and all of the evens above 500k will be at least 1 more than all the evens below it)

Daddy

Code Select
#!/usr/bin/python
# Filename : Euler_014.py
# Which starting number, under one million, produces the longest chain?
from time import time
start = time()
longestChain = 0
chainValue = 0
#Calculate and return the length of a chain
def chain(n):
lengthChain= 0
while n>1:
if n%2==0:
n/=2
else:
n= (3*n)+1
lengthChain+=1
return lengthChain
#Check up to 1 million.
for i in range(500001,1000000, 2):
if chain(i) > longestChain:
longestChain = chain(i)
chainValue=i
#Time to print shit
print chainValue
print chain(longestChain)
print time() - start


If I changed my function to
Code Select
def chain(n):
lengthChain= 0
while n>1:
if n%2:
n= (3*n)+1
else:
n/=2
lengthChain+=1
return lengthChain

It will run about one second faster, but it doesn't look as neat.

Daddy

February 26, 2009, 12:20:42 PM #116 Last Edit: February 26, 2009, 12:48:03 PM by Raekewn
With Guffnigger's help I turned:
ValenteMac:Python jamesvalente$ python Euler_010.py
142913828922
29.1039888859
ValenteMac:Python jamesvalente$

into:

ValenteMac:Desktop jamesvalente$ python Euler_010.py
142913828922
3.5488049984
ValenteMac:Desktop jamesvalente$

Between 9 and 10 times faster.  baddood;

Further optimization:

ValenteMac:Python jamesvalente$ python Euler_010.py
142913828922
2.34858393669
ValenteMac:Python jamesvalente$

Daddy

I think Guff and I are the only people who post in this thread. baddood;

guff

Quote from: Raekewn on February 26, 2009, 02:16:51 PM
I think Guff and I are the only people who post in this thread. baddood;
those nerds  madood;

Ezloļŗ•

:)

Go Up