0

hi, guide me,how to make a another way to write again

a=1
c=0
d=0
b=1
num1=1
e=1
while b <1000:
    b=a*3
    a=a+1
    c=b+c
while num1<994:
    num1=e*5
    e=e+1
    d=num1+d
    
print "Here is the sum of multipile by 3 and 5 is %d"%(c+d)
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by tzushky
0

I think we'd all like to know what exactly the problem is: shortening the code? making it more "profesisonal-like", make functions, ....?

I think you must do the sum of say, all numbers from 1 to x , multiplied by 3, (sum<1000) and same by 5...

Honestly:
s = 1*3 +2*3+...n*3 = (1+2+3+...n)*3 and since s<1000 => 1+2+3+...n< (1000/3)=333

In case you didn't know, 1+2+3+...n = n*(n+1) /2 => n*(n+1) = 666=> n^2 +n - 666 = 0

Delta = 1-4*(1)*(-666) = 2665

=> possible n is = (-1 + sqrt(2665))/2 = 25

So

For your sum multiple by 3 :

sum3 = sum(range(26))*3    #Because range(26) will take numbers from 0 to 25=n

And similarly sum5, only a different number will end:

1+...+m < 1000/5 = 200

m(m+1)<400 => m^2+m-400 = 0 is your equation....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.