• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

find the sum of all the numbers between two numbers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried?  What happened?
 
Anton Plotnikov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want write this function.
what code do I need to insert?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I apologize.
 
Campbell Ritchie
Marshal
Posts: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apology accepted
 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 5582
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 5582
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it matter here whether a is smaller or larger than b?

Good catch
 
Campbell Ritchie
Marshal
Posts: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 42
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 5582
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or even much easier:

 
Campbell Ritchie
Marshal
Posts: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Or even much easier . . .

Easier?
 
Piet Souris
Saloon Keeper
Posts: 5582
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



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

 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 5582
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80066
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now we just need the Big-O analysis of space and time complexity of each algorithm.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic