

Ivo_Timóteo Escreveu:(...)
Mas como saber que não há números maiores que N que satisfazem a condição? Que N é esse?
Embora ligeiramente fora do âmbito do Quark!, podem tentar demonstrarÉ mesmo muito fácil
"""
Project Euler 30
Sum all numbers that are equal to the sum of the mth power of its digits
@jap-2009 having fun with Quark!
"""
m = 5
def sumdigits(n,m):
"sum of the mth power of the digits of n"
return sum(map(lambda c: int(c)**m,str(n)))
# Wild guess that the maximum number of digits is, at most, 10 and try to refine your guess to a smaller number...
for ndigits in range(1,10):
if ndigits*9**m < 10**ndigits: break
else:
print "Outch, the numbers can be really huge!!!"
# Now we hopefully know an upper bound for the number of digits...
print "Maximum number of digits to search", ndigits
print "Please wait..."
L = filter(lambda n: n == sumdigits(n,m), xrange(10,10**ndigits))
print "The numbers are", L
print "and their sum is", sum(L)
hexphreak Escreveu:Por acaso está tecnicamente errada, já que não prova que não existam números com mais de dez dígitos para os quais a condição se verifiqueMas é extremamente fácil mostrar que apenas números com até seis dígitos o podem fazer...
Bruno Oliveira Escreveu:Prof. de facto tanto o 0 como o 1, satisfazem as condições do problema, mas eles dizem para não contar com o 1, pois é uma soma de dígitos, então a resposta é 443839.Asis not a sum it is not included.
tharis@tharis-lp:~$ time ./test
real 1m10.905s
user 1m1.104s
sys 0m0.156s
int main() {
unsigned long long i;
const unsigned long long l=10000000000ULL;
for (i=0;i<l;i++);
return 0;
}
import psyco
psyco.full()
tharis@tharis-lp:~$ time python test.py
Maximum number of digits to search 6
Please wait...
The numbers are [4150, 4151, 54748, 92727, 93084, 194979]
and their sum is 443839
real 0m11.984s
user 0m11.517s
sys 0m0.052s
tharis@tharis-lp:~$ time python test.py
Maximum number of digits to search 6
Please wait...
The numbers are [4150, 4151, 54748, 92727, 93084, 194979]
and their sum is 443839
real 0m2.779s
user 0m1.760s
sys 0m0.168s
"""
Project Euler 30
Sum all numbers that are equal to the sum of the mth power of its digits
@jap-2009 having fun with Quark!
Modifications by Tharis: dictionary `d' and `sumdigits' function
"""
m = 5
d= dict( [(i,i**m) for i in range(10)] )
def sumdigits(n,m):
"sum of the mth power of the digits of n"
global d
d[n]=d[n/10]+d[n%10]
return d[n]
# Wild guess that the maximum number of digits is, at most, 10 and try to refine your guess to a smaller number...
for ndigits in range(1,10):
if ndigits*9**m < 10**ndigits: break
else:
print "Outch, the numbers can be really huge!!!"
# Now we hopefully know an upper bound for the number of digits...
print "Maximum number of digits to search", ndigits
print "Please wait..."
L = filter(lambda n: n == sumdigits(n,m), xrange(10,10**ndigits))
print "The numbers are", L
print "and their sum is", sum(L)
Utilizadores a navegar neste fórum: Nenhum utilizador registado e 1 visitante