-
Notifications
You must be signed in to change notification settings - Fork 1
/
57_SumOfFirstNNaturalNumber.java
70 lines (57 loc) · 1.56 KB
/
57_SumOfFirstNNaturalNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
/**
* This program calculates the sum of the first 'n' natural numbers using different approaches.
* It demonstrates three methods: using loops, recursion, and a direct mathematical formula.
*/
public class SumOfFirstNNaturalNumber {
/**
* 1st Approach: Using Loops
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
/*
public static int naturalSum(int n) {
int sum = 0;
for(int i=1; i<=n; i++) {
sum += i;
}
return sum;
}
*/
/**
* 2nd Approach: Using Recursion
*
* Time Complexity: O(n)
* Space Complexity: O(n) - due to recursion stack
*/
/*
public static int naturalSum(int n) {
if(n == 0) {
return 0;
}
return (n + naturalSum(n-1));
}
*/
/**
* 3rd Approach: Using Formula
*
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
public static int naturalSum(int n) {
return ((n * (n + 1)) / 2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
// Approach 1: Using Loops
// System.out.println("Sum = " + naturalSum(n));
// Approach 2: Using Recursion
// System.out.println("Sum = " + naturalSum(n));
// Approach 3: Using Formula
System.out.println("Sum = " + naturalSum(n));
sc.close();
}
}