Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Finding the sum of all numbers between two negative numbers
New Posts  All Forums:Forum Nav:

Finding the sum of all numbers between two negative numbers

post #1 of 21
Thread Starter 
I have to create a robust algorithm that calculates the sum of two numbers. I've been at this all night (not that question, but performing tasks) and this one has got my brain falling out a little.

I previously already had the algorithm for two positive numbers working great. And for two negative numbers, I use the same solution. However first I make the two numbers positive, then at the end I convert them back to a negative again ( total = total - (total * 2); ) since adding two negative results in a negative number. This works well enough.

However, where I'm stuck is finding the sum of all the numbers between a negative and a positive. For instance, the sum of all numbers between -50 and 50.

I'm not looking for code as this is an assignment but perhaps some hints on how to achieve this would be great! Maths isn't my super strong point.
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #2 of 21
If I've got what you want to do right, I think you're trying to sum an arithmetic series - http://mathcentral.uregina.ca/QQ/database/QQ.02.06/jo1.html

For a negative sequence, swap the signs so S=( N(N-1) ) / 2
Edited by BorisTheSpider - 12/12/12 at 4:57pm
Project Obselete
(20 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k @ 4.6GHz Asus Maximus IV Extreme R9 290 R9 290 
RAMHard DriveHard DriveHard Drive
16GB G-Skill DDR3 1600MHz 256GB Samsung 830 SSD 512GB Samsung 830 SSD 256 GB Samsung PB22-J SSD 
Hard DriveHard DriveOptical DriveCooling
1TB Samsung Spinpoint F3 2TB Samsung Spinpoint F4 Asus BW-12B1LT Custom loop with external radbox 
OSMonitorMonitorMonitor
Windows 7 Ultimate x64 Crossover 27Q LED-P Crossover 27Q LED-P Optoma GT720 
PowerCaseAudioOther
Corsair AX1200i Coolermaster HAF X Asus Xonar D2X Highpoint RocketRAID 2680 
  hide details  
Reply
Project Obselete
(20 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k @ 4.6GHz Asus Maximus IV Extreme R9 290 R9 290 
RAMHard DriveHard DriveHard Drive
16GB G-Skill DDR3 1600MHz 256GB Samsung 830 SSD 512GB Samsung 830 SSD 256 GB Samsung PB22-J SSD 
Hard DriveHard DriveOptical DriveCooling
1TB Samsung Spinpoint F3 2TB Samsung Spinpoint F4 Asus BW-12B1LT Custom loop with external radbox 
OSMonitorMonitorMonitor
Windows 7 Ultimate x64 Crossover 27Q LED-P Crossover 27Q LED-P Optoma GT720 
PowerCaseAudioOther
Corsair AX1200i Coolermaster HAF X Asus Xonar D2X Highpoint RocketRAID 2680 
  hide details  
Reply
post #3 of 21
Just a small nit pick, a better way to turn an integer negative is to multiply by -1 smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #4 of 21
Thread Starter 
Quote:
Originally Posted by BorisTheSpider View Post

If I've got what you want to do right, I think you're trying to sum an arithmetic series - http://mathcentral.uregina.ca/QQ/database/QQ.02.06/jo1.html
For a negative sequence, swap the signs so S=( N(N-1) ) / 2

Oh my, that's actually quite handy. For one, I wasn't using that. I was using a while loop with this:
Code:
public void Sum(int a, int b) 
   {     
       int total = 0;
       int value = a;

      while(value < b) 
               {    
                   total += value;
                   System.out.println(total);
                   value++;
               }

The code now looks like:
Code:
public int sum2(int a, int b)
   {
       int sum = b * (b + a) / 2;
       return sum;
   }

Which is much shorter!

I'll test the negatives shortly.

I wonder though, how do you do a positive and a negative together?
Quote:
Originally Posted by tompsonn View Post

Just a small nit pick, a better way to turn an integer negative is to multiply by -1 smile.gif

Again, that's handy as well!
Edited by Rubers - 12/12/12 at 5:26pm
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #5 of 21
The sum of every integer between and including a and b is (a+b)*(a-b+1)/2
I THINK, early here. Not sure if it will work with a negative and positive integer... try it tongue.gif

smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #6 of 21
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

The sum of every integer between and including a and b is (a+b)*(a-b+1)/2
I THINK, early here.
smile.gif

Ok, strange results with these two.
Code:
sum = b * (b + a) / 2;
gives 5050 where a = 1 and b =100.

but
Code:
sum = b * (b - a) / 2;

gives -4949 where a = -1 and b =-100.

thompsonn's
Code:
sum = (a + b) * (a - b + 1) /2;

gives -4949 wherea = 1 and b = 100.

but gives -5050 where a = -1 and b = -100!
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #7 of 21
I think it could be: for the sum of every integer between a and b inclusive: ((a+b)/2)(a+b)
based on: (n/2)(a(sub 1) + a(sub n))
    
CPUMotherboardGraphicsRAM
Intel 4770k Asus Z87 Deluxe/Quad EVGA 1080 TI FE G. Skill 
RAMHard DriveHard DriveCooling
G.Skill Western Digital Samsung 840 Pro Water Cooled 
OSMonitorMonitorKeyboard
Windows 10 Pro Samsung 27" LED Acer 22" LCD Saitek Eclipse II 
PowerCaseMouse
Silver Stone 850W Antec Lanboy Air Microsoft Wireless 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 4770k Asus Z87 Deluxe/Quad EVGA 1080 TI FE G. Skill 
RAMHard DriveHard DriveCooling
G.Skill Western Digital Samsung 840 Pro Water Cooled 
OSMonitorMonitorKeyboard
Windows 10 Pro Samsung 27" LED Acer 22" LCD Saitek Eclipse II 
PowerCaseMouse
Silver Stone 850W Antec Lanboy Air Microsoft Wireless 
  hide details  
Reply
post #8 of 21
Yeah I buggered something there I think tongue.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #9 of 21
Thread Starter 
It's ok, I got this:
Code:
public int sum2(int a, int b)
   {
       int sum = 0;
       
       if(a > 0 && b > 0)
       {
            sum = b * (b + a) / 2;
       }
       
       if(a < 0 && b < 0)
       {
           a = -a;
           b = -b;
           sum = b * (b + a) / 2;
           sum = sum * -1;
       }
       return sum;
   }

Or, there's this for the negatives. I'm not sure which is better:
Code:
       if(a < 0 && b < 0)
       {
           sum = b * (b - a) / 2;
           sum = sum - (a - b * -1);
           sum = sum * -1;
       }
I think I like the first better, though. It seems a bit cleaner even if it is one more line.
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
The Riginator
(20 items)
 
 
Wife's Rig
(5 items)
 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon S600 Adreno 330 2GB LPDDR3 NAND Storage 
Hard DriveOSMonitorKeyboard
Samsung 32GB MicroSD Android 4.4.2 KitKat 5 inch (441ppi) 1080x1920 Super AMOLED SwiftKey 
Power
2600mAh Battery 
  hide details  
Reply
post #10 of 21
S = n[2a+(n-1)d]/2 is entirely general, with the following provisos:

1. Always start at the smaller number - that is to say, if summing negative numbers, start with the most negative, and if crossing from -ve to +ve, start with the -ve one.

2. If the sequence has zero as a term, make sure you count that when calculating n

3. Don't make the first or last term zero - start with the first nonzero term and end with the last nonzero term

In pig-pseudo-c:

a = insert a
b = insert b
d = insert difference between terms

// make d +ve

d=abs(d)

// set a < b

if b < a
{
temp=a
a=b
b=temp
}

// Fix a=0

if a==0
{
if b<0 then a=a-d
else
a=a+d
}

// fix b=0

if b==0
{
b=b-d
}

// get the number of terms

n = abs( (b-a)/d ) + 1

// sum them

sum = n(2a+(n-1)d)/2


Needs testing - may contain traces of bogosity
Edited by BorisTheSpider - 12/12/12 at 6:47pm
Project Obselete
(20 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k @ 4.6GHz Asus Maximus IV Extreme R9 290 R9 290 
RAMHard DriveHard DriveHard Drive
16GB G-Skill DDR3 1600MHz 256GB Samsung 830 SSD 512GB Samsung 830 SSD 256 GB Samsung PB22-J SSD 
Hard DriveHard DriveOptical DriveCooling
1TB Samsung Spinpoint F3 2TB Samsung Spinpoint F4 Asus BW-12B1LT Custom loop with external radbox 
OSMonitorMonitorMonitor
Windows 7 Ultimate x64 Crossover 27Q LED-P Crossover 27Q LED-P Optoma GT720 
PowerCaseAudioOther
Corsair AX1200i Coolermaster HAF X Asus Xonar D2X Highpoint RocketRAID 2680 
  hide details  
Reply
Project Obselete
(20 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k @ 4.6GHz Asus Maximus IV Extreme R9 290 R9 290 
RAMHard DriveHard DriveHard Drive
16GB G-Skill DDR3 1600MHz 256GB Samsung 830 SSD 512GB Samsung 830 SSD 256 GB Samsung PB22-J SSD 
Hard DriveHard DriveOptical DriveCooling
1TB Samsung Spinpoint F3 2TB Samsung Spinpoint F4 Asus BW-12B1LT Custom loop with external radbox 
OSMonitorMonitorMonitor
Windows 7 Ultimate x64 Crossover 27Q LED-P Crossover 27Q LED-P Optoma GT720 
PowerCaseAudioOther
Corsair AX1200i Coolermaster HAF X Asus Xonar D2X Highpoint RocketRAID 2680 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Finding the sum of all numbers between two negative numbers