Solved

sum challenge

Posted on 2014-12-16
12
55 Views
Last Modified: 2015-01-12
Hi,

I saw one challenge where i need to find
1+1/2+1/3+1/4+.........+1/1000

I could not get much idea on how to proceed to find this sum. I see denominator pattern increasing 1 to 1000 where as numerator remails same as 1.

Please advise. thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 40503329
Something like this perhaps:

float t =1;
float u=1;

for(int f=0;f<1000;f++){
u += 1/t++;
System.out.println(u);
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40503334
You might use a for loop.
Accuracy may be better if you sum the smaller terms first, but don't worry too much about that until you have at least something working.
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 250 total points
ID: 40503852
Correction to my startup code

float t =1;
float u=1;

for(int f=0;f<1000;f++){
u += 1/++t;
System.out.println(u);
}

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40503884
Now you're summing
1+1/2+1/3+1/4+.........+1/1001
It might be less confusing to use
        float u=0;
        for(int f=1;f<=1000;f++){
            u += 1.0/f;
        }
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40503890
Yes, that looks better. It was a quick sketch, with not 100% attention to the bounds. But you're right.

----

and how about : for(int f=1;f<1001;f++){
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40503901
gudii

what they are asking you to do here is simply to add up a sequence of numbers, but if they were just integers, it would be kind of boring . . . adding 1,2,3,4,5,6, .... 1000.

Instead, you are asked to start with 1, and add fractions of 1 to it. So the first iteration has 1 + 1/2 (which is 0.5), so the first iteration's result is 1.5. Then to that, you next add 1/3 (which is 0.3333), and then 1/4, then 1/5 etc etc etc, and finally to add 1/1000th to the penultimate result to produce a final total.

There are probably better and more elegant ways of achieving this than my code demonstrates.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40526310
   for(int f=1;f<=1000;f++){

what is difference of above line with below

: for(int f=1;f<1001;f++){

Open in new window


There are probably better and more elegant ways of achieving this than my code demonstrates.

i wonder what are better ways. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40526328
Those two lines are identical in effect.
So the difference would be mainly a matter of stylistic preference.

Visually, one might appear more directly connected to
1+1/2+1/3+1/4+.........+1/1000

To me, code that more closely parallels its specification can make it easier to spot errors in following that specification.

Better ways may be ways that more correctly implement the specified sum.
If having to keep track of the relationship between t and f can be a potential source of error, then one might consider that a point of inelegance.
On the other hand, avoiding implicit conversions from int to float may be a consideration for you.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40534075
To me, code that more closely parallels its specification can make it easier to spot errors in following that specification.
which one does that

If having to keep track of the relationship between t and f can be a potential source of error, then one might consider that a point of inelegance.
i do not think we need any relationship between t and f right?

avoiding implicit conversions from int to float may be a consideration for you.

how to avoid this.
please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40534094
which one does that
If neither strikes you as any more connected to the specification than another, then this would not be a consideration for you.
To me, since the original specification mentions the number 1000,  and does not mention the number 1001, it seems more natural for the code to do the same.
Others may want to consider different stylistic preferences.


any relationship between t and f
Versions of the code that do not include f have no relationships regarding f.


how to avoid this.
Versions of the code that do include f,  maintaining it as a float and never assigning an int to it, can avoid conversions from int to float.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40534351
Versions of the code that do include f,  maintaining it as a float and never assigning an int to it, can avoid conversions from int to float.

Open in new window


do we need float in denominator for f. I thought we only need int.
please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40534440
(int)1/(int)1000 == 0
I don't think that was what the sum challenge intended
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question