1

I'm currently trying to solve some problems in Project Euler. As part of the first question, I need to find the sum of all the multiples of 5 under 1000. Here's what I wrote:

fiveSum = 5   #the sum of the multiples
n = 1.0    #number of the multiples
i = 5    #represents the current multiple of 5

while (i + 5 < 1000):
    i = i * (1 + (1 / n)) 
    fiveSum += i 
    n += 1 

When it's done, for some reason n=200, when it should be 199. When i=995.0, the boolean expression still equals "true". I tried adding:

print i + 5 < 1000 
print i + 5 
print 995.0 + 5 < 1000 

to see what does the computer think when i=995.0, and it says:

True
1000.0
False

Does anyone have any idea what am I missing? When I do a similar loop for multiples of 3 it comes out fine.

Koala
  • 78
  • 5
  • Welcome to StackOverflow. I get `False`, `1000.0`, `False` with python2. – Tim Nov 29 '19 at 16:20
  • 1
    You should NOT use exact comparisons with floating point operations. 0.3<0.2+0.1 will return true. All these numbers are integer, you don't need to treat them as float – WNG Nov 29 '19 at 16:23
  • Thank you @Tim @WNG, I didn't know about that strange behaviour, good to learn. In regards to the float, if n isn't a float type, the `i = i * (1 + (1 / n))` part doesn't behave properly (with multiples of 3 at least) – Koala Nov 29 '19 at 16:36
  • I am not quite sure, but are you looking for `print(sum(i for i in range(1000) if i%5 == 0))` ? – Patrick Artner Nov 29 '19 at 16:38
  • @PatrickArtner, indeed, but for now I'm trying to understand how 995+5 is smaller then 1000 – Koala Nov 29 '19 at 16:46
  • [Broken floating point](https://stackoverflow.com/q/588004/3545273) strikes again!.. `5 * (1.0 / 5)` is not guaranteed to be exactly 1.0 because of floating point inaccuracy. More explainations and detail in the linked post. – Serge Ballesta Nov 29 '19 at 17:59

1 Answers1

0

your counter has start value 1 : n = 1 , after first iteration your counter is 2 (after n += 1 ) Because of that in the end you have counter for one too much.

your counter should start from 0, than you will have division with 0 in first iteration, because of that you need to reorder your code:

fiveSum = 5   #the sum of the multiples
n = 0    # n = 0.0. number of the multiples
i = 5    #represents the current multiple of 5

while (i + 5 < 1000):

    fiveSum += i
    n += 1
    i = i * (1 + (1 / n))

print (n)

output:

199 # 199.0
ncica
  • 7,015
  • 1
  • 15
  • 37
  • Please insist on the fact that you don't use floats for you comparisons. – Tim Nov 29 '19 at 16:27
  • @Tim for example if I put n = 0.0 ,in this case result will be 199.0 – ncica Nov 29 '19 at 16:30
  • I get what you're saying, the problem is that this loop works fine with multiples of 3. – Koala Nov 29 '19 at 16:40
  • 1
    @koala The loop works fine for 3 because 1000 is not divisible by 3 – Patrick Artner Nov 29 '19 at 16:43
  • @PatrickArtner, I'm not sure how it's related to `i+5<1000` being true for `i=995.0` – Koala Nov 29 '19 at 16:51
  • I cant' reproduce your result with your example, but it may help you understand a little better anwers on this link https://stackoverflow.com/questions/13479163/round-float-to-x-decimals, there are explinations how float numbers behave in memory – ncica Nov 29 '19 at 17:12