-3

I need someone to give me an idea on how to go on about this problem.Using a loop to calculate the fraction , There is no common value.I want to get the sum

Eg for fraction :

1  1/5  1/10  1/15  1/20 … 1/290  1/295  1/300

code snippet:-

int sum=0;
for(int i=1;i<=60 ;i++)
{
   int sum=1
}
Lucifer
  • 1,594
  • 2
  • 18
  • 32
  • 9
    You want to get the sum of `1/(5*i)` for `i` in `1:60`? You know that's a geometric series, which means there's a [formula](https://en.wikipedia.org/wiki/Geometric_series#Formula) to get the result instantly – Rafalon May 31 '18 at 09:14
  • 2
    To start with, the sum is not an integer, so you probably want a `float`. Beyond that, it's not clear what the question is. – John Perry May 31 '18 at 09:16
  • @JohnPerry I would suggest decimal for such accurate values instead of float – Ipsit Gaur May 31 '18 at 09:18
  • 1
    @IpsitGaur Fair enough; I'm not that familiar with C#, but in any case there's a problem with `int sum`. – John Perry May 31 '18 at 09:19
  • 1
    @IpsitGaur: What makes you think that a decimal type can store the terms more accurately than a `double`? – Bathsheba May 31 '18 at 09:19
  • 3
    @Rafalon - it's not geometric! Geometric series' denominators increase exponentially. This is increasing linearly. – Alex Reinking May 31 '18 at 09:37
  • @AlexReinking that's what I realized 3 minutes ago as my comment on Bathsheba's answer demonstrates. But hey, thanks for pointing it out anyway – Rafalon May 31 '18 at 09:38

3 Answers3

5

These sort of problems are actually surprisingly non-trivial due to issues with working with floating point, and decimal types for that matter.

Accepting that you want a loop solution for this (a closed form solution for n terms does exist), first note that your series can be written as

1 + 1/5(1 + 1/2 + 1/3 + ... + 1/60)

Then note that a good rule of thumb when working with floating point types is to add the small terms first.

So an algorithm would be of the form

double sum = 0.0;
for (int i = 60; i >= 1; --i){
    sum += 1.0 / i;
}
sum = sum / 5 + 1;

Note the 1.0 in the numerator; that's there to defeat integer division.

Reference: Is floating point math broken?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

͏Since you asked for a hint:

float sum = 1.0;
for (int i = 5; i <= ??; i += ??) {
    sum += 1.0/i;
}

What goes in place of the ??s?

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • 1
    It's naughty to add the large terms first when working with floating point. – Bathsheba May 31 '18 at 09:20
  • @Bathsheba - It doesn't matter one iota in this case. The relative error between the two directions is `2.27e-16` – Alex Reinking May 31 '18 at 09:26
  • But it's still bad practice. If this finds its way into a language where the `double` allows you freedom to mess about with the exponent and mantissa, it *could* matter. – Bathsheba May 31 '18 at 09:27
  • 1
    I'm not really looking to discuss the finer points of floating-point best practice when OP is having trouble forming the loop in the first place. But you raise a fair point. – Alex Reinking May 31 '18 at 09:33
  • @Bathsheba interesting... Could you explain "why" this is a bad practice? Or give some reference to read, I would like to learn more about that :) – Damien Flury May 31 '18 at 09:43
  • 1
    ^ Floating point is most accurate when adding numbers of similar magnitude. In floating point `1 + 1e-16` is exactly `1`, but you can actually express `1e-16 + 1e-16`. If you have a lot of small values, they could add up to a bigger one, which would be more accurate if the terms of your sum have a high dynamic range. – Alex Reinking May 31 '18 at 09:47
  • 1
    @DamienFlury: in floating point, adding a very small number to a very big number can be a no-op. So you want to give the small numbers chance in a sum of contributing to the final total. Therefore add them first. – Bathsheba May 31 '18 at 09:48
  • @AlexReinking and Bathsheba Alright, I think this makes sense, thank you! – Damien Flury May 31 '18 at 09:58
1

try this code:

    double sum=1;
    for(int i=5; i<=300; i+=5)
        sum += (double) 1 / i;

The value of sum will be 1.93597408259035

Saeed Bolhasani
  • 552
  • 3
  • 15