Open In App

Find sum of odd factors of a number

Last Updated : 14 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, the task is to find the odd factor sum.
Examples : 
 

Input : n = 30
Output : 24
Odd dividers sum 1 + 3 + 5 + 15 = 24
Input : 18
Output : 13
Odd dividers sum 1 + 3 + 9 = 13

 

Approach :- Naive approach in o(n) time complexity

The first approach involves iterating over all factors of the given number and checking if each factor is odd. If a factor is odd, it is added to the running sum of odd factors.

  Begin by including the necessary header files, including iostream, which will allow us to perform input and output operations.
  Define a function named sumOfOddFactors that takes an integer n as input and returns an integer as output.
  Initialize an integer variable sum to zero, which will be used to accumulate the sum of odd factors of the input integer.
  Use a for loop to iterate through all integers from 1 to n, incrementing by 1 at each iteration.
  Check if the current integer i is a factor of n by using the modulo operator (%). If the remainder of n divided by i is zero, then i is a factor of n.
  Check if the current factor i is odd by using the modulo operator (%). If the remainder of i divided by 2 is 1, then i is odd.
  If i is both a factor of n and odd, then add i to the sum variable.
  After the loop has completed, return the final value of sum.
  In the main function, initialize an integer variable n to the value 30.
  Call the sumOfOddFactors function with n as input and output the result to the console.
  Return 0 to indicate successful program execution.

C++




#include <iostream>
 
using namespace std;
 
int sumOfOddFactors(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0 && i % 2 == 1) {
            sum += i;
        }
    }
    return sum;
}
 
int main() {
    int n = 30;
    cout << sumOfOddFactors(n) << endl; // Output: 8
    return 0;
}


Java




import java.util.*;
 
public class Main {
  public static int sumOfOddFactors(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (n % i == 0 && i % 2 == 1) {
        sum += i;
      }
    }
    return sum;
  }
  public static void main(String[] args) {
    int n = 30;
    System.out.println(sumOfOddFactors(n)); // Output: 8
  }
 
}


Python3




def sumOfOddFactors(n):
    sum = 0
    for i in range(1, n+1):
        if n % i == 0 and i % 2 == 1:
            sum += i
    return sum
 
if __name__ == '__main__':
    n = 30
    print(sumOfOddFactors(n))  # Output: 8


C#




using System;
 
class Program
{
    // Function to calculate the sum of odd factors of a number n
    static int SumOfOddFactors(int n)
    {
        int sum = 0;
         
        // Iterate through numbers from 1 to n
        for (int i = 1; i <= n; i++)
        {
            // Check if i is a factor of n and i is odd
            if (n % i == 0 && i % 2 == 1)
            {
                sum += i; // Add i to the sum
            }
        }
        return sum;
    }
 
    static void Main()
    {
        int n = 30;
        Console.WriteLine(SumOfOddFactors(n)); // Output: 8
    }
}


Javascript




// Function to calculate the sum of odd factors of a number
function sumOfOddFactors(n) {
    let sum = 0;
    for (let i = 1; i <= n; i++) {
        // Check if 'i' is a factor of 'n' and if 'i' is odd
        if (n % i === 0 && i % 2 === 1) {
            sum += i; // Add 'i' to the sum
        }
    }
    return sum; // Return the sum of odd factors
}
 
const n = 30; // Define the value of 'n'
console.log(sumOfOddFactors(n)); // Output the result of sumOfOddFactors


Output

24



Time complexity – O(N)

Auxiliary Space – O(1)

Prerequisite : Sum of all the factors of a number
As discussed in above mentioned previous post, sum of factors of a number is 
Let p1, p2, … pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2)* … (pkak)
 

Sum of divisors = (1 + p1 + p12 ... p1a1) * 
(1 + p2 + p22 ... p2a2) *
.............................................
(1 + pk + pk2 ... pkak)

To find sum of odd factors, we simply need to ignore even factors and their powers. For example, consider n = 18. It can be written as 2132 and sum of all factors is (1)*(1 + 2)*(1 + 3 + 32). Sum of odd factors (1)*(1+3+32) = 13.
To remove all even factors, we repeatedly divide n while it is divisible by 2. After this step, we only get odd factors. Note that 2 is the only even prime.
 

C++




// Formula based CPP program
// to find sum of all
// divisors of n.
#include <bits/stdc++.h>
using namespace std;
 
// Returns sum of all factors of n.
int sumofoddFactors(int n)
{
    // Traversing through all
    // prime factors.
    int res = 1;
 
    // ignore even factors by
    // removing all powers of
    // 2
    while (n % 2 == 0)
        n = n / 2;
 
    for (int i = 3; i <= sqrt(n); i++)
    {
 
        // While i divides n, print
        // i and divide n
        int count = 0, curr_sum = 1;
        int curr_term = 1;
        while (n % i == 0) {
            count++;
 
            n = n / i;
 
            curr_term *= i;
            curr_sum += curr_term;
        }
 
        res *= curr_sum;
    }
 
    // This condition is to handle
    // the case when n is a prime
    // number.
    if (n >= 2)
        res *= (1 + n);
 
    return res;
}
 
// Driver code
int main()
{
    int n = 30;
    cout << sumofoddFactors(n);
    return 0;
}


Java




// Formula based Java program
// to find sum of all divisors
// of n.
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Returns sum of all
    // factors of n.
    static int sumofoddFactors(int n)
    {
        // Traversing through
        // all prime factors.
        int res = 1;
     
        // ignore even factors by
        // removing all powers
        // of 2
        while (n % 2 == 0)
            n = n / 2;
     
        for (int i = 3; i <= Math.sqrt(n); i++)
        {
     
            // While i divides n, print i
            // and divide n
            int count = 0, curr_sum = 1;
            int curr_term = 1;
            while (n % i == 0)
            {
                count++;
     
                n = n / i;
     
                curr_term *= i;
                curr_sum += curr_term;
            }
     
            res *= curr_sum;
             
        }
     
        // This condition is to handle
        // the case when n is a
        // prime number.
        if (n >= 2)
            res *= (1 + n);
     
        return res;
    }
     
    // Driver code
    public static void main(String args[])
                        throws IOException
    {
        int n = 30;
        System.out.println(sumofoddFactors(n));
    }
}
 
/* This code is contributed by Nikita Tiwari.*/


Python3




# Formula based Python3 program
# to find sum of all divisors
# of n.
import math
 
# Returns sum of all factors
# of n.
def sumofoddFactors( n ):
     
    # Traversing through all
    # prime factors.
    res = 1
     
    # ignore even factors by
    # of 2
    while n % 2 == 0:
        n = n // 2
     
    for i in range(3, int(math.sqrt(n) + 1)):
         
        # While i divides n, print
        # i and divide n
        count = 0
        curr_sum = 1
        curr_term = 1
        while n % i == 0:
            count+=1
             
            n = n // i
            curr_term *= i
            curr_sum += curr_term
         
        res *= curr_sum
     
    # This condition is to
    # handle the case when
    # n is a prime number.
    if n >= 2:
        res *= (1 + n)
     
    return res
 
# Driver code
n = 30
print(sumofoddFactors(n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// Formula based C# program to
// find sum of all divisors of n.
using System;
 
class GFG {
     
    // Returns sum of all
    // factors of n.
    static int sumofoddFactors(int n)
    {
        // Traversing through
        // all prime factors.
        int res = 1;
     
        // ignore even factors by
        // removing all powers
        // of 2
        while (n % 2 == 0)
            n = n / 2;
     
        for (int i = 3; i <= Math.Sqrt(n); i++)
        {
            // While i divides n, print i
            // and divide n
            int count = 0, curr_sum = 1;
            int curr_term = 1;
            while (n % i == 0)
            {
                count++;
                n = n / i;
     
                curr_term *= i;
                curr_sum += curr_term;
            }
     
            res *= curr_sum;
        }
     
        // This condition is to handle
        // the case when n is a
        // prime number.
        if (n >= 2)
            res *= (1 + n);
     
        return res;
    }
     
    // Driver code
    public static void Main(String[] argc)
    {
        int n = 30;
        Console.Write(sumofoddFactors(n));
    }
}
 
/* This code is contributed by parashar...*/


Javascript




<script>
 
// Formula based Javascript program
// to find sum of all
// divisors of n.
 
// Returns sum of all factors of n.
function sumofoddFactors(n)
{
    // Traversing through all
    // prime factors.
    let res = 1;
 
    // ignore even factors by
    // removing all powers of
    // 2
    while (n % 2 == 0)
        n = n / 2;
 
    for (let i = 3; i <= Math.sqrt(n); i++)
    {
 
        // While i divides n, print
        // i and divide n
        let count = 0;
        let curr_sum = 1;
        let curr_term = 1;
        while (n % i == 0)
        {
            count++;
 
            n = n / i;
 
            curr_term *= i;
            curr_sum += curr_term;
        }
 
        res *= curr_sum;
    }
 
    // This condition is to
    // handle the case when
    // n is a prime number.
    if (n >= 2)
        res *= (1 + n);
 
    return res;
}
 
// Driver code
let n = 30;
document.write(sumofoddFactors(n));
 
// This code is contributed by _saurabh_jaiswal
 
</script>


PHP




<?php
// Formula based PHP program
// to find sum of all
// divisors of n.
 
// Returns sum of all factors of n.
function sumofoddFactors($n)
{
    // Traversing through all
    // prime factors.
    $res = 1;
 
    // ignore even factors by
    // removing all powers of
    // 2
    while ($n % 2 == 0)
        $n = $n / 2;
 
    for ($i = 3; $i <= sqrt($n); $i++)
    {
 
        // While i divides n, print
        // i and divide n
        $count = 0; $curr_sum = 1;
        $curr_term = 1;
        while ($n % $i == 0)
        {
            $count++;
 
            $n = $n / $i;
 
            $curr_term *= $i;
            $curr_sum += $curr_term;
        }
 
        $res *= $curr_sum;
    }
 
    // This condition is to
    // handle the case when
    // n is a prime number.
    if ($n >= 2)
        $res *= (1 + $n);
 
    return $res;
}
 
// Driver code
$n = 30;
echo sumofoddFactors($n);
 
// This code is contributed
// by nitin mittal.
?>


Output

24



Time complexity: O(sqrt(n))

Auxiliary Space: O(1)
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads