How to find the sum of the first 'N' natural numbers without using any loops and recursion in Java?
Anit Sarker
Senior QE and Automation Lead @EY GDS | ex-TCS | SDET | Automation and Functional QA | Selenium | RESTAssured | JAVA | Cucumber | 6x TOSCA Certified | Agile | BDD | ISTQB Certified | Azure 1x | Photography Enthusiast
Calculating the sum of first N natural numbers is very easy in mathematics. The formula is very simple and that is -
Sn = n(n+1)/2, where n is the nth of the series.
S1 = 1(1+1)/2 = 1
S2 = 2(2+1)/2 = 3
S3 = 3(3+1)/2 = 6
.
.
Sn = n(n+1)/2
In Java, it is still easy to implement the above formula using loops. This can also be done using recursions. But here we will see how it can be done without using any of these.
We will be introduced to ‘reduce’ methods from the Java collection framework, and the ‘BinaryOperator<T>’ functions interface.
‘reduce’ is used on streams and the basic function of this method is to return a single output based the several inputs. Example: the problem statement itself. Where we are passing N number of integers and output is singular.
‘BinaryOperator<T>’ is a functional interface, which takes single input. It used the concept of the accumulator along with the ‘reduce’ method.
Let’s see a simple implementation of BinaryOperator<T> :
BinaryOperator<Integer> getsum = (acc, x) -> acc + x;
Here we can see that BinaryOperator<T> takes single input x, but the accumulator (acc) is coming from the reduce method’s identity parameters. The Identity parameter is passed to the BinaryOperator<T> as the accumulator.
Now, we know we need to accumulate the sum of all the integers up to N, hence we can try to use reduce method here. So let’s see how we can do it.
I have recorded the code implementation and uploaded it to my YouTube channel. Please check it out for a better understanding.
YT Video link: https://youtu.be/8BpRhRDQlRg
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.BinaryOperator; public class FindSumOfFirstXNaturalNumbers { public static void main(String[] args) { /* Find the sum of the first X natural numbers. X =100, 50, 1000 any integer Constraints: We cannot use Loops and Recursions */ //input Integer[] listOfNumbers = new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; //Sum of first n integers is = n(n+1)/2 //n =15 //Solution 15(15+1)/2= 15 * 8 = 120 //Solution of sum from 1 to 100 = 100(100+1)/2 = 5050 List<Integer> newIntegerList = new ArrayList<>(); // This list will hold values from 1 to 100 for(int i = 1; i<=100; i++){ newIntegerList.add(i); } //Solution List<Integer> intList = new ArrayList<>(Arrays.asList(listOfNumbers)); BinaryOperator<Integer> getSum = (acc, x) -> acc+x; int solution = intList .stream() .reduce(0, getSum); //0 is sent as accumulator to BinaryOperator and sets the starting value System.out.println("The solution is " + solution); // Solution = 120 System.out.println("\n*********************************************\n"); int solution1 = newIntegerList .stream() .reduce(0, getSum); System.out.println("The solution is " + solution1); // Solution = 5050 } }