Question: Given an integer N, find all the prime factors of the number.


    Input:
    N = 45

    Output: 3 5

    Finding prime factors of any numbers is a very important concept in number theory. One sample problem is available here in which we have to find the largest prime factor.

    The mathematical concept behind this problem is simple. We start with ‘2’ and keep dividing the original number to get rid of composite factors. Continue this process until the number is no longer divisible by 2. This means that at this point, we do not have any multiples of 2 in the factors.

    Example: 48 = 2 * 2 * 2 * 2 * 3
    If we divide it by 2 four times we get rid of all the composite factors.

    • Now we proceed with ‘3’ and so on to get rid of all the factors.
    • Keep on adding these numbers to a set as they are left out.
    • The reason these numbers are left out is because they are prime numbers.
    • Hence, at the end of our loop we have all our prime factors in the set.

    The code shown below implements the above algorithm.

    import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;
    
    public class PrimeFactorisation {
    
      /**
       * Created by studyalgorithms.com
       */
    
      public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        Set<Long> primeFactors = new HashSet<>();
    
        while (N % 2 == 0) {
          N /= 2;
          primeFactors.add(2L);
        }
    
        for (long j = 3; j <= Math.sqrt(N); j += 2) {
          while (N % j == 0) {
            N /= j;
            primeFactors.add(j);
          }
        }
    
        if (N > 2)
          primeFactors.add(N);
    
        for (Long primeFactor : primeFactors) {
          System.out.print(primeFactor + " ");
        }
    
      }
    }
    Code language: Java (java)

    A working implementation of the code can be found here.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We have several ways of finding prime numbers. Some of the methods are discussed in the these posts. Method 1 Method 2 Method 3 In this post we will find the first N prime numbers using the Sieve of Eratosthenes. This technique is helpful in scenarios when we have to give immediate results and we need to query the prime numbers …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the sum of even Fibonacci numbers.

    by nikoo28
    3 minutes read

    Question: Find the sum of even fibonacci numbers upto a limit N. Input:100Output: 44 Fibonacci numbers are a miracle of Math and are defined as:- Thus the Fibonacci series can be given as0, 1, 1, 2, 3, 5, 8, 13, 21… Let us try to understand the question. We need to find all the Fibonacci numbers till a limit ‘N’ and then sum only the even numbers in them.Example:N = 10 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8 Sum of even numbers: 2 + 8 = 10 N …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given two strings, determine if they share a common sub-string. If they have a common sub-string, print YES else print NO. Input:studyalgorithmsalgosOutput: YES This is one of the easy problems but can get a little tricky, if we do not understand it completely. Let us try to understand the test caseString 1 = “studyalgorithms” String 2 = “algos”The sub-string “algo” is common between both the sub-strings and therefore the answer is “YES“. Let us try to take one more test case. String 1 = “hello” String 2 = “world”The …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    [Hackerrank] – Multiples of 3 and 5

    by nikoo28
    3 minutes read

    Question: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below ‘N’ Input: N = 100 Output: 2318 The most naive approach to solve this problem will be Iterate over each number till N If the number is divisible by 3 or 5, add it to the sum Print the sum This approach can work well to numbers …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [Hackerrank] – Palindrome Index

    by nikoo28
    3 minutes read

    Question: Given a string, identify the index of character to be removed to change the string into a palindrome. If the string cannot be converted to palindrome or is already a palindrome just return -1. Input: abckdeedcbaOutput: 3 (0 based indexing) To start off with the problem, we must first understand that there can be two possible ways to make the string a palindrome. If the given string is “bcbc”. If we remove the first ‘b’, the string becomes “cbc” which is a palindrome. If we remove the last ‘c’, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Let us try to simplify the problem as much as we can. We are given with some of the terms. Weights:- According to the problem each character in the English alphabet has been assigned a weight. ‘a’ has been given a weight of 1, ‘b’ has a weight of 2, and so on. The weight of ‘z’ is 26. All the letters are assumed to be lowercase for simplicity. Weight of a string:- This is defined as the sum of all the characters in the string. Example if a string …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More