Java Number Sum sumFromNtoM(int n, int m)

Here you can find the source of sumFromNtoM(int n, int m)

Description

Returns the sum from n to m (Ex: n = 1, m = 3 => 1 + 2 + 3 = 6)

License

Open Source License

Declaration

public static int sumFromNtoM(int n, int m) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w w  w .  j a va2 s  . co  m*/
     * Returns the sum from n to m (Ex: n = 1, m = 3 => 1 + 2 + 3 = 6)
     */
    public static int sumFromNtoM(int n, int m) {
        return Math.abs(m - n + 1) * (m + n) / 2;
    }
}

Related

  1. sumDivisors(int n)
  2. sumError(long samples, long count, double m2, double mean)
  3. sumFirstIntegers(final long i)
  4. sumFirstN(long n)
  5. sumFirstNDivisibleByM(long n, long m)
  6. sumHadOverflow(long a, long b, long sum)
  7. sumHash(int hash, String element)
  8. sumHash(int leftHash, int rightHash)
  9. sumIf(final Integer number, final Integer sum, final Boolean condition)