• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

find the sum of all the numbers between two numbers  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How tp solve this task?
Given two integers a and b, which can be positive or negative,
find the sum of all the numbers between including them too
and return it. If the two numbers are equal return a or b.
Note: a and b are not ordered!

Thanks.
 
Sheriff
Posts: 5029
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?  What happened?
 
Anton Plotnikov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want write this function.
what code do I need to insert?
 
author
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anton Plotnikov wrote:i want write this function.
what code do I need to insert?



Knute asked what have you done with this homework assignment so far... basically, we can't tell what kinda help you need, without knowing what you are confused with, and of course, this requires knowing what you did so far.

Henry
 
Ranch Hand
Posts: 423
2
Chrome Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an easy question to answer, even for someone fairly new to Java like me.

It's a homework assignment, and a pretty easy one at that.  Read your textbook.
 
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JV, I don't think that post of yours does anything to help AP.

AP: welcome to the Ranch
Do you remember from maths at school how to total progressions of numbers?
 
Jim Venolia
Ranch Hand
Posts: 423
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I apologize.
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted
 
Rancher
Posts: 89
13
Chrome Eclipse IDE Java MySQL Database Scala Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anton, for this problem I think you might want to consider thinking about the steps to go through counting the numbers, write those down. Next, you need to match code to achieve these steps you have written. I would suggest using your textbook and looking up for loops.

See what you can get from this point and then post what you need help on if you have anymore questions.

-Zach
 
Ranch Hand
Posts: 118
1
Chrome IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anton Plotnikov wrote:i want write this function.
what code do I need to insert?



Okay, lets work together.

Before we jump start writing the code lets look at the steps we need to take to solve the problem. You can write these steps in many different way: flow chart, pseudo code etc. I like pseudo code because it's more similar to written description. You can also add code in a pseudo code for brevity which I find very handy. For example, I can write a := 0 which means, variable a is set to value zero. Here is the pseudo code for GetSum function,

// start of function GetSum
Function GetSum(Number1, Number2):
    summation := 0
    In a loop
        summation := summation + Number1
        Increment Number1
        Check if Number1 == Number2
              True: break the loop
              False: continue the loop
    return summation
// end of function GetSum

Different language provides different ways to write this function. For example, Java has three different loop constructs: For, while, do-while. Which one do you think would work best for our situation? You also know need to know how to use variables since you need to store the result of the summation somewhere. You would also need to know how to use use arithmetic operator to modify the value stored in a variable.

Once you read those tutorials try writing the function. Post here if you are stuck or have trouble reading the java tutorials. (Note that this function doesn't do exactly what you want, it just gives you a starting point.)

         
       
       
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quazi Irfan wrote:. . . Before we jump start writing the code lets look at the steps we need to take to solve the problem. . . .

That is a good start but then you go straight on to pseudocode for loops. How do you know you will want loops? Isn't that jumping ahead a bit?
 
Quazi Irfan
Ranch Hand
Posts: 118
1
Chrome IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How do you know you will want loops? Isn't that jumping ahead a bit?



Most people would repeatedly add numbers to get a sum of a range. I am assuming he knows that much. And I am instructing him to repeat the same tasks in a programming language.

Is that what you are asking?
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quazi Irfan wrote:. . . Most people would repeatedly add numbers to get a sum of a range. . . .

But there is a more efficient way to total those numbers. We don't know how much the OP knows, nor whether he wants efficiency or whether he has been told to write a loop.
 
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words: wait for a reply from OP, whether he has gotten some ideas yet.
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . wait . . . whether he has gotten some ideas yet.

. . . or whether the deadline for this assignment has passed
 
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anton Plotnikov,
Welcome to Code ranch. I think the problem is simple, but here's a clause that's missed by others above :

Anton Plotnikov wrote:
Note: a and b are not ordered!


This means that you might need to swap the parameters if a is greater than b
With regards to the solution, are you familiar with IntStream ? Hint : There are 2 methods in the IntStream interface that can help you with your assignment.
 
Quazi Irfan
Ranch Hand
Posts: 118
1
Chrome IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But there is a more efficient way to total those numbers.



Can you tell me more about it?
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula for the series 1 + 2 + 3 + ... + n is well known. Try to deduce a similar formula for the similar series from a to b, inclusive. Does it matter here whether a is smaller or larger than b?
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it matter here whether a is smaller or larger than b?

Good catch
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lakshman Arun wrote:Does it matter here whether a is smaller or larger than b? . . .

Yes, but the corrections required for a loop will be more than those required for a simple formula.
 
salvin francis
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:...but here's a clause that's missed by others above ...


It's rude on my part to simply assume all have missed reading that. I am sorry about that.



Lakshman Arun wrote:Does it matter here whether a is smaller or larger than b?


Let me demonstrate with a program:
 
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Hi Anton Plotnikov,
Welcome to Code ranch. I think the problem is simple, but here's a clause that's missed by others above :

Anton Plotnikov wrote:
Note: a and b are not ordered!


This means that you might need to swap the parameters if a is greater than b
With regards to the solution, are you familiar with IntStream ? Hint : There are 2 methods in the IntStream interface that can help you with your assignment.



And also, one or more of the boundary numbers might be negative. Not sure if all the loop structures will accept negative integers?
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ras oscar wrote:. . . . Not sure if all the loop structures will accept negative integers?

They all will if you threaten to hit them if they don't.
 
salvin francis
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ras oscar wrote:...Not sure if all the loop structures will accept negative integers?


Does this solve your doubt ?
output:
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or even much easier:

 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Or even much easier . . .

Easier?
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. yes.

Since OP STILL hasn't replied, I was afraid we were still writing things too difficult. So I thought about this easy way, and put in a smiley as well, to convince everyone of my sincerity.
 
salvin francis
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Or even much easier: ...



Adding 2 lines to your code more makes it easier to read:

 
Saloon Keeper
Posts: 9134
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Salvin. The increment statement of a loop should be as simple as possible, which in this case is achieved by determining the range to iterate over before the loop starts.
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright, I confess.

Since OP didn't react to all the simple things we wrote, I tried to be funny. But the mrs is right, she always tell me I have no talent at all for being funny. So from now on I'll stick to being the old grumpy man I always was.   
 
Campbell Ritchie
Marshal
Posts: 59745
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since OP hasn't come back, I have my own take on that:-No need for a loop. Only possible error: overflow. Alternative version:-At least I think those methods are correct.
 
Knute Snortum
Sheriff
Posts: 5029
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Since OP didn't react to all the simple things we wrote, I tried to be funny. But the mrs is right, she always tell me I have no talent at all for being funny. So from now on I'll stick to being the old grumpy man I always was.   


In your defence, you did put a winky-face on the post.
 
salvin francis
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Campbell has posted, the sum of an arithmetic progression is

n  x  ( (A1 + An) ÷ 2 )

Where
n is the total number of elements,
A1 is the minimum
An is the maximum

Our progression has a common difference of 1, hence its easy to calculate n given the min and max :
n = (An - A1) + 1

 
Ranch Hand
Posts: 239
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now we just need the Big-O analysis of space and time complexity of each algorithm.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!