└── Basic Maths ├── ArmstrongNumbers.java ├── CheckForPrime.java ├── CheckPalindrome.java ├── CountDigits.java ├── HCF.java ├── PrintAllDivisors.java └── ReverseNumber.java /Basic Maths/ArmstrongNumbers.java: -------------------------------------------------------------------------------- 1 | public class ArmstrongNumbers { 2 | 3 | // Method to check if a number is an Armstrong number 4 | static boolean isArmstrong(int n) { 5 | int original = n; 6 | int sum = 0; 7 | int digits = String.valueOf(n).length(); // Count the number of digits 8 | 9 | while (n > 0) { 10 | int digit = n % 10; // Get the last digit 11 | sum += Math.pow(digit, digits); // Raise it to the power of the number of digits and add to sum 12 | n /= 10; // Remove the last digit from n 13 | } 14 | 15 | return original == sum; // Check if the original number is equal to the sum 16 | } 17 | 18 | public static void main(String[] args) { 19 | int N = 153; // Example Armstrong number 20 | System.out.println("Number: " + N); 21 | boolean isArmstrong = isArmstrong(N); 22 | System.out.println("Is Armstrong: " + isArmstrong); 23 | } 24 | } 25 | 26 | // 27 | // Output: 28 | // Number: 153 29 | // Is Armstrong: true 30 | // Time Complexity: O(d) where d is the number of digits in the number 31 | // Space Complexity: O(1) as it uses a constant amount of space 32 | -------------------------------------------------------------------------------- /Basic Maths/CheckForPrime.java: -------------------------------------------------------------------------------- 1 | 2 | public class CheckForPrime { 3 | 4 | static boolean checkPrime(int n){ 5 | int cnt = 0; 6 | for(int i = 1; i <= Math.sqrt(n); i++){ 7 | if(n % i == 0){ 8 | cnt = cnt + 1; 9 | if(n / i != i){ 10 | cnt = cnt + 1; 11 | } 12 | } 13 | } 14 | if(cnt == 2){ 15 | return true; 16 | } 17 | else{ 18 | return false; 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | int n = 1483; 24 | boolean isPrime = checkPrime(n); 25 | if(isPrime){ 26 | System.out.println(n + " is a prime number."); 27 | } else { 28 | System.out.println(n + " is not a prime number."); 29 | } 30 | } 31 | } 32 | 33 | // Output: 34 | // 1483 is a prime number. 35 | // Time Complexity: O(sqrt(n)), where n is the input number 36 | // Space Complexity: O(1) as it uses a constant amount of space -------------------------------------------------------------------------------- /Basic Maths/CheckPalindrome.java: -------------------------------------------------------------------------------- 1 | public class CheckPalindrome { 2 | 3 | // Method to check if a number is a palindrome using arithmetic operations 4 | static boolean isPalindrome1(int n) { 5 | int original = n; 6 | int reversed = 0; 7 | while (n > 0) { 8 | int digit = n % 10; // Get the last digit 9 | reversed = reversed * 10 + digit; // Append it to the reversed number 10 | n /= 10; // Remove the last digit from n 11 | } 12 | return original == reversed; // Check if the original number is equal to the reversed number 13 | } 14 | 15 | public static void main(String[] args) { 16 | int N = 12321; 17 | System.out.println("Number: " + N); 18 | boolean isPalindrome = isPalindrome1(N); 19 | System.out.println("Is Palindrome: " + isPalindrome); 20 | } 21 | } 22 | 23 | // 24 | // Output: 25 | // Number: 12321 26 | // Is Palindrome: true 27 | // Time Complexity: O(d) where d is the number of digits in the number 28 | // Space Complexity: O(1) as it uses a constant amount of space 29 | 30 | // Leetcode Problem: https://leetcode.com/problems/palindrome-number/ -------------------------------------------------------------------------------- /Basic Maths/CountDigits.java: -------------------------------------------------------------------------------- 1 | // This code demonstrates two methods to count the number of digits in a given integer N. 2 | // The first method uses logarithmic properties, while the second method uses a loop to divide the number by 10 until it reaches zero. 3 | // Count the number of digits in a number using two different methods 4 | 5 | public class CountDigits { 6 | 7 | static int countDigits1(int n) { 8 | int cnt = (int) (Math.log10(n) + 1); 9 | return cnt; 10 | } 11 | 12 | static int countDigits2(int n) { 13 | int cnt = 0; 14 | while (n > 0) { 15 | n /= 10; 16 | cnt++; 17 | } 18 | return cnt; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int N = 329823; 23 | System.out.println("N: " + N); 24 | int digits1 = countDigits1(N); 25 | int digits2 = countDigits2(N); 26 | System.out.println("Number of Digits in N: " + digits1 + " and " + digits2); 27 | } 28 | } 29 | // Output: 30 | // N: 329823 31 | // Number of Digits in N: 6 and 6 32 | // Time Complexity: O(log N) for countDigits1, O(d) for countDigits2 where d is the number of digits -------------------------------------------------------------------------------- /Basic Maths/HCF.java: -------------------------------------------------------------------------------- 1 | public class HCF { 2 | // Method to calculate the HCF (Highest Common Factor) of two numbers using the Euclidean algorithm 3 | static int hcf(int a, int b) { 4 | while (b != 0) { 5 | int temp = b; 6 | b = a % b; // Remainder 7 | a = temp; // Update a to the previous b 8 | } 9 | return a; // The HCF is stored in a 10 | } 11 | 12 | public static void main(String[] args) { 13 | int num1 = 48; 14 | int num2 = 18; 15 | System.out.println("HCF of " + num1 + " and " + num2 + " is: " + hcf(num1, num2)); 16 | } 17 | } 18 | 19 | //Euclidean algorithm is an efficient method for computing the HCF of two integers. 20 | // It works by repeatedly replacing the larger number with the remainder of the division of the two numbers until one of them becomes zero. 21 | // output: 22 | // HCF of 48 and 18 is: 6 23 | // Time Complexity: O(log(min(a, b))) where a and b are the two numbers 24 | // Space Complexity: O(1) as it uses a constant amount of space 25 | 26 | // Leetcode Problem: https://leetcode.com/problems/greatest-common-divisor-of-strings/ -------------------------------------------------------------------------------- /Basic Maths/PrintAllDivisors.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | public class PrintAllDivisors { 5 | public static ArrayList findDivisors(int n) { 6 | 7 | ArrayList divisors = new ArrayList<>(); 8 | int sqrtN = (int) Math.sqrt(n); 9 | 10 | for (int i = 1; i <= sqrtN; ++i) { 11 | // Check if i divides n 12 | // without leaving a remainder 13 | if (n % i == 0) { 14 | divisors.add(i); 15 | if (i != n / i) { 16 | divisors.add(n / i); 17 | } 18 | } 19 | } 20 | return divisors; 21 | } 22 | 23 | public static void main(String[] args) { 24 | int number = 12; 25 | ArrayList divisors = findDivisors(number); 26 | 27 | System.out.print("Divisors of " + number + " are: "); 28 | for (int divisor : divisors) { 29 | System.out.print(divisor + " "); 30 | } 31 | System.out.println(); 32 | } 33 | } 34 | 35 | // Output: 36 | // Divisors of 12 are: 1 12 2 6 3 4 37 | // Time Complexity: O(sqrt(n)), where n is the input number 38 | // Space Complexity: O(d), where d is the number of divisors found 39 | -------------------------------------------------------------------------------- /Basic Maths/ReverseNumber.java: -------------------------------------------------------------------------------- 1 | public class ReverseNumber { 2 | 3 | // Method to reverse a number using arithmetic operations 4 | static int reverse2(int n) { 5 | int reversed = 0; 6 | while (n > 0) { 7 | int digit = n % 10; // Get the last digit 8 | reversed = reversed * 10 + digit; // Append it to the reversed number 9 | n /= 10; // Remove the last digit from n 10 | } 11 | return reversed; 12 | } 13 | 14 | // Method to reverse a number using long to handle overflow and Integer limits 15 | static int reverse1(int x) { 16 | long sum=0; 17 | while(x!=0) 18 | { 19 | sum= sum*10 + (x%10); 20 | x=x/10; 21 | } 22 | if(sum > Integer.MAX_VALUE || sum