Skip to content
Related Articles

Related Articles

Find sum of values of N at each step after repeatedly dividing it with its smallest factor

Improve Article
Save Article
  • Last Updated : 02 Mar, 2022
Improve Article
Save Article

Given an integer N, the task is to find the sum of all values of N after repeatedly dividing it with its smallest factor.

Examples:

Input: N = 5
Output: 6
Explanation: Initially N = 5. The smallest factor of N is 5 itself. Therefore, the value of N will become N/5 = 5/5 = 1. Hence, the sum of all values of N is 5 + 1 = 6.

Input: N = 10.
Output: 16
Explanation: Initially N = 10. The smallest factor of N is 2. Therefore, the value of N will become N/2 = 10/2 = 5. Similarly, in next step, N = N/5 = 5/5 = 1. Hence, the sum of all values of N is 10 + 5 + 1 = 16.

 

Approach: The given problem is an implementation-based problem and can be solved by iterating in the range [2, √N] and dividing N with its smallest factor, maximum possible times. Maintain the sum of all values of N in a variable which will be the required answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum of all the
// values of N after repeatedly dividing
// it with its smallest factor
int sumValues(int N)
{
    // Stores the required answer
    int ans = N;
 
    int i = 2;
 
    // Loop to iterate over
    // the factors of N
    while (N > 1) {
 
        // If i is a factor
        if (N % i == 0) {
 
            // Update N
            N = N / i;
 
            // Update ans
            ans += N;
        }
        else {
            i++;
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver function
int main()
{
    int N = 10;
    cout << sumValues(N);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the sum of all the
  // values of N after repeatedly dividing
  // it with its smallest factor
  static int sumValues(int N)
  {
    // Stores the required answer
    int ans = N;
 
    int i = 2;
 
    // Loop to iterate over
    // the factors of N
    while (N > 1) {
 
      // If i is a factor
      if (N % i == 0) {
 
        // Update N
        N = N / i;
 
        // Update ans
        ans += N;
      }
      else {
        i++;
      }
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver function
  public static void main (String[] args) {
    int N = 10;
    System.out.println(sumValues(N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# python3 program for the above approach
 
# Function to find the sum of all the
# values of N after repeatedly dividing
# it with its smallest factor
def sumValues(N):
 
    # Stores the required answer
    ans = N
 
    i = 2
 
    # Loop to iterate over
    # the factors of N
    while (N > 1):
 
        # If i is a factor
        if (N % i == 0):
 
            # Update N
            N = N // i
 
            # Update ans
            ans += N
 
        else:
            i += 1
 
    # Return Answer
    return ans
 
# Driver function
if __name__ == "__main__":
 
    N = 10
    print(sumValues(N))
 
# This code is contributed by rakeshsahni


C#




// C# program for the above approach
using System;
class GFG
{
   
// Function to find the sum of all the
// values of N after repeatedly dividing
// it with its smallest factor
static int sumValues(int N)
{
   
    // Stores the required answer
    int ans = N;
 
    int i = 2;
 
    // Loop to iterate over
    // the factors of N
    while (N > 1) {
 
        // If i is a factor
        if (N % i == 0) {
 
            // Update N
            N = N / i;
 
            // Update ans
            ans += N;
        }
        else {
            i++;
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver function
public static void Main()
{
    int N = 10;
    Console.Write(sumValues(N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
      // JavaScript code for the above approach
 
      // Function to find the sum of all the
      // values of N after repeatedly dividing
      // it with its smallest factor
      function sumValues(N) {
          // Stores the required answer
          let ans = N;
 
          let i = 2;
 
          // Loop to iterate over
          // the factors of N
          while (N > 1) {
 
              // If i is a factor
              if (N % i == 0) {
 
                  // Update N
                  N = Math.floor(N / i);
 
                  // Update ans
                  ans += N;
              }
              else {
                  i++;
              }
          }
 
          // Return Answer
          return ans;
      }
 
      // Driver function
 
      let N = 10;
      document.write(sumValues(N));
 
     // This code is contributed by Potta Lokesh
  </script>


 
 

Output

16

 

Time Complexity: O(√N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!