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 - Page 2

post #11 of 21
Thread Starter 
Quote:
Originally Posted by BorisTheSpider View Post

S = n[2a+(n-1)d]/2 is entirely general, provided that you make d negative when the numbers are negative. (n is the number of terms, a is the first term, d is the difference between terms). Don't make a=0, if the sequence starts there - start with the first non-zero term.
It even works if the sequence goes from negative to positive, but then n must include zero as a term - eg, going from -2 to +3, n=6.
Excuse my ignorant, pig-pseudo-C, but it's be something like:
(Define the variables)
a =
b =
d =
(we sort so B is bigger (in absolute terms) than A)
if abs(b) < abs(a) then
{
temp=b
b=a
a=temp
}
(Then we make d negative if the whole sequence is composed of negative numbers)
if (a < 0 && b < 0) then d=-d;
(make sure a is not zero and if it is, make it the first non-zero term of the sequence instead)
if a == 0
{
a = a+d
}
(now calculate the number of terms)
n = abs(b-a);
(now sum the sequence)
sum = n(2a+(n-1)d)/2;

I'm not sure I understand what d is supposed to be frown.gif

Here's what I have so far:
Code:
public int sum(int a, int b)
   {
       int sum = 0;
       
       //If both numbers are positive
       if(a > 0 && b > 0)
       {
           //If a is greater than b, swap them
           if( a > b)
           {
               
               int temp = a;
               a = b;
               b = temp;
           }
           
           sum = b * (b + a) / 2;
       }
       
       //If both numbers are negative        
       if(a < 0 && b < 0)
       {
           a = -a;
           b = -b;
           sum = b * (b + a) / 2;
           sum = sum * -1;
       }
           
       //If both numbers are the same
       if(a == b)
       {
            sum = a;
       }
       return sum;
   }

I was trying an algorithm including turning the negative into a positive, working out all the sums between 0 and it, turning it back into a negative and then adding it to the positive but that doesn't seem to work well.
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 #12 of 21
for numbers 0 to n, excluding 0 and n. therefore in between 0 and n:

postive numbers: T(n) = [n(n+1)/2] - n

negative numbers: T(n) = [n(n-1)/2] + n


so say a is positive and b is negative...

sum of all numbers = {[b(b-1)/2] + b} + {[a(a+1)/2] - a}

then simplified:

sum of all numbers = (a^2 - a + b^2 + b)/2


im not sure if you wanted to add the negatives onto the postives rather then letting them subtract from the positives, if not then this should do it.:

sum of all numbers = (a^2 - a - b^2 - b)/2


I hope that helps, im only working off high school maths though.
    
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Gigabyte GA-Z68XP-UD4 Gigabyte GTX 970 G1 Gaming G-Skill Ripjaws 1600MHz 8gb (2x4gb)  
Hard DriveHard DriveHard DriveHard Drive
Sandisk Extreme 240GB WD Green 500GB WD Blue 500GB Seagate 500GB 
Optical DriveCoolingOSMonitor
Samsung DVD Noctua NH-D14 Windows 7 Home Premium x64 Crossover 27Q LED-P 
MonitorKeyboardPowerCase
ASUS VS248 Ducky Shine Zero D2108S (Blue-LED) (Cherry MX B... Corsair TX-750 Silverstone Raven RV01 
MouseMouse PadAudio
Logitech G9x Roccat Taito Audio Technica ATH-M50 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Gigabyte GA-Z68XP-UD4 Gigabyte GTX 970 G1 Gaming G-Skill Ripjaws 1600MHz 8gb (2x4gb)  
Hard DriveHard DriveHard DriveHard Drive
Sandisk Extreme 240GB WD Green 500GB WD Blue 500GB Seagate 500GB 
Optical DriveCoolingOSMonitor
Samsung DVD Noctua NH-D14 Windows 7 Home Premium x64 Crossover 27Q LED-P 
MonitorKeyboardPowerCase
ASUS VS248 Ducky Shine Zero D2108S (Blue-LED) (Cherry MX B... Corsair TX-750 Silverstone Raven RV01 
MouseMouse PadAudio
Logitech G9x Roccat Taito Audio Technica ATH-M50 
  hide details  
Reply
post #13 of 21
I'm not sure I understand what d is supposed to be frown.gif

Go back and re-read, I fixed my post - the one you quoted was badly broken, but I thought about it and I think I cracked it this time.

d is the delta between terms, it's just d=1 if you want to sum all integers between the two numbers, but the formula is more general - you can have any size gap between terms, it doesn't have to be 1.
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 #14 of 21
Thread Starter 
What about this:
Code:
if(a < 0 && b > 0 || a > 0 && b < 0)
       {
           if( a > b)
           {
               
               int temp = a;
               a = b;
               b = temp;
           }
           
           int valueForA = a;
           int valueForB = b;
           int totalForA = 0;
           int totalForB = 0;
           
           while(valueForA < 0)
           {
               totalForA += valueForA;
               valueForA++;
           }
           
          
           while(valueForB > 0)
           {
               totalForB += valueForB;
               valueForB--;
           }
           
           sum = totalForA + totalForB;
       }

Seems to work.
If I add a = -5 and b = 10;

5- + -4 + -3 + -2 + -1 = -15
1+2+3+4+5+6+7+8+9+10 = 55

55 - 15 = 40

or

-15 +1+2+3+4+5+6+7+8+9+10 = 40
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 #15 of 21
I don't get what's the problem...
Code:
public static int sum(int a, int b)
{
int sum = 0
if (a == b)
{
return 0;
}
if (a < b)
{
for(;a < b;a++)
{
sum += a
}
}
else
{
for(;b < a;b++)
{
sum += b
}
}
return sum
}
post #16 of 21
Thread Starter 
I neded up going with this:
Code:
public int sum(int a, int b)
   {
       int sum = 0;
       //If either value is the negative of itself then save the time and return zero
       if(a == b * -1 || b == a * -1)
       {
            return sum = 0;    
       }
       
       //If both numbers are positive
       if(a > 0 && b > 0)
       {
           //If a is later than b, swap them
           if( a > b)
           {
               
               int temp = a;
               a = b;
               b = temp;
           }
           
           sum = b * (b + a) / 2;
       }
       
       //If both numbers are negative        
       if(a < 0 && b < 0)
       {
           a = -a;
           b = -b;
           sum = b * (b + a) / 2;
           sum = sum * -1;
       }
       
       if(a < 0 && b > 0 || a > 0 && b < 0)
       {
           int sumA = 0; // Temp variable for working out a
           int sumB = 0; // Temp variable for working out b
           
           //Check that a is the smallest integer
           //If not, swap them around
           if( a > b)
           {
               
               int temp = a;
               a = b;
               b = temp;
           }
           
           a = -a; //Make a positive
           sumA = a * (a + 1) / 2; //Calculate the sum of all the numbers between 1 and a     
           sumA = sumA * -1; // Make a negative again
           sumB = b * (b + 1) / 2; //Calculate the sum of all the numbers between 1 and b        
           sum = sumA + sumB; //Add the two results together
       }
       
       //If both numbers are the same
       if(a == b)
       {
            sum = a;
       }
       return sum;
   }

All problems solve smile.gif
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 #17 of 21
this really makes me want to take programing
post #18 of 21
Thread Starter 
I also had to create code to look for the first 50 prime numbers. But, instead, I went for as many prime number as you can throw at your machine!
Code:
public boolean checkPrimeNumber(int a, int b) 
    {
        int divisible = 0;
        int index = 1;
        while(index < b && divisible < 3)
        {
            if(a % index == 0)
            {
                divisible++;
            }
            index++;
        }
        if(divisible == 2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
   public void printPrimes(int a, int b)
   {
       for(int index = a ;  index < b ; index++)
       {
           if(checkPrimeNumber(index, b))
           {
               System.out.println(index);
           }
       }
   }

It starts to slow down a little if you throw a billion as the second integer, but it works well biggrin.gif
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 #19 of 21
Nice.

For more on finding primes, testing for primality, factoring and more check out "Bruce Schneier - Applied Cryptography". A classic if ever there was one.
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 #20 of 21
OK, I've made a few programs on finding primes.

A few things:
[*] Don't check for evens after 2!
[*] If you can try and keep a list of all primes. All non-primes can be made from prime factors.
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