Java Programs for Common Algorithms
Java Programs for Common Algorithms
The program computes prime factors of a given number by using a loop to test consecutive integers starting from 2. For each integer, it checks if it divides the number evenly; if so, it prints and continues dividing the number by the prime until it's no longer divisible. After the loop, if the remaining number is greater than 1, it's also a prime factor and is printed. This method ensures prime factors are printed in ascending order and prevents unnecessary computations beyond the square root of the number, thus optimizing the factorization process .
The Java program identifies 3-digit Armstrong numbers in a range by using a loop to iterate over each number within the specified range. For each number, it calculates the sum of the cubes of its digits by extracting each digit using modulus arithmetic and division. If the sum equals the original number, it is printed as an Armstrong number. This verification process relies on the property that an Armstrong number for 3 digits is equal to the sum of the cubes of its digits .
The Java program identifies and counts different character types in a sentence by iterating through each character and checking its type based on specific conditions. It uses predefined checks for vowels, consonants, digits, and spaces using character comparison and built-in Java methods like `Character.isLetter`, `Character.isDigit`, and `Character.isWhitespace`. A separate counter is maintained for vowels, consonants, digits, spaces, and any other character types, which are then printed at the end of the iteration .
The Java code limits the input number for generating prime factors by returning -1 for any number less than or equal to 1. This limitation prevents attempting to factorize numbers that do not have meaningful prime factors, such as zero and negative numbers. This validation ensures that only valid inputs are processed, maintaining the integrity of the prime factorization process and avoiding unnecessary computations .
The output format of the Fibonacci number generation program presents the Fibonacci sequence as a space-separated string of numbers up to the Nth Fibonacci number. The program checks for input errors by testing if the input number N is less than or equal to zero, in which case it outputs -1, indicating an invalid input. This error handling prevents generating an incorrectly defined Fibonacci sequence and ensures the logic runs only for valid, positive integer inputs .
The Java code sorts an array using the Bubble Sort technique, which repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated for each element in the array, with each pass through the array ensuring that at least one element is placed in its correct position. The number of swaps is calculated by incrementing a swap counter each time two elements are swapped. This count is printed at the end to denote the number of swaps performed during the sort .
The program generates the first N Fibonacci numbers using an iterative approach. It initializes the first two Fibonacci numbers as 0 and 1, then iteratively calculates the next Fibonacci number by summing the last two numbers in the sequence. This is done using a loop that continues until N numbers are generated. The constraint enforced is that N must be positive, otherwise it returns -1. This ensures incorrect inputs do not lead to errors in execution .
The Java program adds two binary strings by iterating from the end of both strings towards the start while maintaining a carry. At each step, it adds corresponding digits from both strings (or zero if out of bounds) and the carry, appending the result's least significant bit to a StringBuilder. It updates the carry to hold the most significant bit of the sum by integer division by two. After processing all digits, any remaining carry is appended, and the result is reversed to form the final binary sum .
The Java code checks if a word is a palindrome by converting the string to lowercase to ensure case insensitivity and then using a two-pointer technique: one starts from the beginning and the other from the end of the string. The characters at these pointers are compared, and the pointers move towards the center. If any character mismatch is found, the word is not a palindrome. If the pointers cross without mismatches, the word is a palindrome .
The Java program determines if a number can be expressed as a sum of consecutive numbers by iteratively checking combinations of potential consecutive numbers. It uses two nested loops; the outer loop varies the length of the sequence, while the inner loop varies the starting number of the sequence. The equation checked is if \(2n = \text{len} \times (\text{len} + 2 \times \text{first} - 1)\) holds true, where \(n\) is the input number, \(\text{len}\) is the length of the sequence, and \(\text{first}\) is the starting number. If true, it prints the sequence. The complexity comes from checking all possible starting points and sequence lengths .