Find sum of odd factors of a number
The problem is to find the sum of all odd factors of a given positive integer. An odd factor of a number 'num' is any positive integer 'i' such that 'num' is divisible by 'i', and 'i' is an odd number. We need to calculate the sum of all such odd factors for a given number 'num'.
Example: Let's take the number 12 to illustrate the problem. The factors of 12 are 1, 2, 3, 4, 6, and 12. The odd factors are 1 and 3. Therefore, the sum of the odd factors of 12 is 1 + 3 = 4.
Pseudocode
1. Start
2. oddFactorSum(num):
3. sum <- 0
4. for i from 1 to num/2:
5. if i is odd and num is divisible by i:
6. print(i, "X", num/i)
7. sum <- sum + i
8. print("Odd Factors Sum:", sum)
9. end oddFactorSum
10. main():
11. oddFactorSum(12)
12. oddFactorSum(50)
13. oddFactorSum(40)
14. end main
15. Stop
Algorithm
- Start the main function.
- Declare a function oddFactorSum that takes an integer 'num' as a parameter.
- Initialize a variable 'sum' to store the sum of odd factors, and set it to 0.
- Use a for loop to iterate through integers from 1 to num/2.
- Inside the loop, check if 'i' is an odd number and if 'num' is divisible by 'i' (i.e., num % i == 0).
- If the conditions in step 5 are satisfied, print the pair of odd factors (i, num/i) in the format (i X num/i).
- Add 'i' to the 'sum'.
- After the loop, print the sum of odd factors with an appropriate message.
- End the oddFactorSum function.
- In the main function, call the oddFactorSum function with different test cases (e.g., 12, 50, 40).
- End the main function.
Code Solution
Here given code implementation process.
// C Program
// Find sum of odd factors of a number
#include <stdio.h>
// Calculates Sum of odd factors of a number
void oddFactorSum(int num)
{
printf("\n Odd Factors pair of a number %d is \n", num);
int sum = 0;
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (i % 2 != 0 && num % i == 0)
{
printf(" (%d X %d) \n", i, num / i);
// Add odd factor
sum += i;
}
}
// Display calculated result
printf(" Odd Factors Sum : %d\n", sum);
}
int main()
{
// Test Cases
oddFactorSum(12);
oddFactorSum(50);
oddFactorSum(40);
return 0;
}
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
/*
Java Program
Find sum of odd factors of a number
*/
public class Factorization
{
// Calculates Sum of odd factors of a number
public void oddFactorSum(int num)
{
System.out.print("\n Odd Factors pair of a number " + num + " is \n");
int sum = 0;
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (i % 2 != 0 && num % i == 0)
{
System.out.print(" (" + i + " X " + num / i + ") \n");
// Add odd factor
sum += i;
}
}
// Display calculated result
System.out.print(" Odd Factors Sum : " + sum + "\n");
}
public static void main(String[] args)
{
Factorization task = new Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
}
}
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find sum of odd factors of a number
*/
class Factorization
{
public:
// Calculates Sum of odd factors of a number
void oddFactorSum(int num)
{
cout << "\n Odd Factors pair of a number " << num << " is \n";
int sum = 0;
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (i % 2 != 0 && num % i == 0)
{
cout << " (" << i << " X " << num / i << ") \n";
// Add odd factor
sum += i;
}
}
// Display calculated result
cout << " Odd Factors Sum : " << sum << "\n";
}
};
int main()
{
Factorization task = Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
return 0;
}
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
// Include namespace system
using System;
/*
C# Program
Find sum of odd factors of a number
*/
public class Factorization
{
// Calculates Sum of odd factors of a number
public void oddFactorSum(int num)
{
Console.Write("\n Odd Factors pair of a number " + num + " is \n");
int sum = 0;
// Execute loop through by 1 to num/2
for (int i = 1; i <= num / 2; ++i)
{
if (i % 2 != 0 && num % i == 0)
{
Console.Write(" (" + i + " X " + num / i + ") \n");
// Add odd factor
sum += i;
}
}
// Display calculated result
Console.Write(" Odd Factors Sum : " + sum + "\n");
}
public static void Main(String[] args)
{
Factorization task = new Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
}
}
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
<?php
/*
Php Program
Find sum of odd factors of a number
*/
class Factorization
{
// Calculates Sum of odd factors of a number
public function oddFactorSum($num)
{
echo "\n Odd Factors pair of a number ". $num ." is \n";
$sum = 0;
// Execute loop through by 1 to num/2
for ($i = 1; $i <= intval($num / 2); ++$i)
{
if ($i % 2 != 0 && $num % $i == 0)
{
echo " (". $i ." X ". intval($num / $i) .") \n";
// Add odd factor
$sum += $i;
}
}
// Display calculated result
echo " Odd Factors Sum : ". $sum ."\n";
}
}
function main()
{
$task = new Factorization();
// Test Cases
$task->oddFactorSum(12);
$task->oddFactorSum(50);
$task->oddFactorSum(40);
}
main();
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
/*
Node Js Program
Find sum of odd factors of a number
*/
class Factorization
{
// Calculates Sum of odd factors of a number
oddFactorSum(num)
{
process.stdout.write("\n Odd Factors pair of a number " + num + " is \n");
var sum = 0;
// Execute loop through by 1 to num/2
for (var i = 1; i <= parseInt(num / 2); ++i)
{
if (i % 2 != 0 && num % i == 0)
{
process.stdout.write(" (" + i + " X " + parseInt(num / i) + ") \n");
// Add odd factor
sum += i;
}
}
// Display calculated result
process.stdout.write(" Odd Factors Sum : " + sum + "\n");
}
}
function main()
{
var task = new Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
}
main();
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
# Python 3 Program
# Find sum of odd factors of a number
class Factorization :
# Calculates Sum of odd factors of a number
def oddFactorSum(self, num) :
print("\n Odd Factors pair of a number ", num ," is ")
sum = 0
i = 1
# Execute loop through by 1 to num/2
while (i <= int(num / 2)) :
if (i % 2 != 0 and num % i == 0) :
print(" (", i ,"X", int(num / i) ,") ")
# Add odd factor
sum += i
i += 1
# Display calculated result
print(" Odd Factors Sum : ", sum )
def main() :
task = Factorization()
# Test Cases
task.oddFactorSum(12)
task.oddFactorSum(50)
task.oddFactorSum(40)
if __name__ == "__main__": main()
Output
Odd Factors pair of a number 12 is
( 1 X 12 )
( 3 X 4 )
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
( 1 X 50 )
( 5 X 10 )
( 25 X 2 )
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
( 1 X 40 )
( 5 X 8 )
Odd Factors Sum : 6
# Ruby Program
# Find sum of odd factors of a number
class Factorization
# Calculates Sum of odd factors of a number
def oddFactorSum(num)
print("\n Odd Factors pair of a number ", num ," is \n")
sum = 0
i = 1
# Execute loop through by 1 to num/2
while (i <= num / 2)
if (i % 2 != 0 && num % i == 0)
print(" (", i ," X ", num / i ,") \n")
# Add odd factor
sum += i
end
i += 1
end
# Display calculated result
print(" Odd Factors Sum : ", sum ,"\n")
end
end
def main()
task = Factorization.new()
# Test Cases
task.oddFactorSum(12)
task.oddFactorSum(50)
task.oddFactorSum(40)
end
main()
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
/*
Scala Program
Find sum of odd factors of a number
*/
class Factorization
{
// Calculates Sum of odd factors of a number
def oddFactorSum(num: Int): Unit = {
print("\n Odd Factors pair of a number " + num + " is \n");
var sum: Int = 0;
var i: Int = 1;
// Execute loop through by 1 to num/2
while (i <= (num / 2).toInt)
{
if (i % 2 != 0 && num % i == 0)
{
print(" (" + i + " X " + (num / i).toInt + ") \n");
// Add odd factor
sum += i;
}
i += 1;
}
// Display calculated result
print(" Odd Factors Sum : " + sum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Factorization = new Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
}
}
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
/*
Swift 4 Program
Find sum of odd factors of a number
*/
class Factorization
{
// Calculates Sum of odd factors of a number
func oddFactorSum(_ num: Int)
{
print("\n Odd Factors pair of a number ", num ," is ");
var sum: Int = 0;
var i: Int = 1;
// Execute loop through by 1 to num/2
while (i <= num / 2)
{
if (i % 2 != 0 && num % i == 0)
{
print(" (", i ," X ", num / i ,") ");
// Add odd factor
sum += i;
}
i += 1;
}
// Display calculated result
print(" Odd Factors Sum : ", sum );
}
}
func main()
{
let task: Factorization = Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
}
main();
Output
Odd Factors pair of a number 12 is
( 1 X 12 )
( 3 X 4 )
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
( 1 X 50 )
( 5 X 10 )
( 25 X 2 )
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
( 1 X 40 )
( 5 X 8 )
Odd Factors Sum : 6
/*
Kotlin Program
Find sum of odd factors of a number
*/
class Factorization
{
// Calculates Sum of odd factors of a number
fun oddFactorSum(num: Int): Unit
{
print("\n Odd Factors pair of a number " + num + " is \n");
var sum: Int = 0;
var i: Int = 1;
// Execute loop through by 1 to num/2
while (i <= num / 2)
{
if (i % 2 != 0 && num % i == 0)
{
print(" (" + i + " X " + num / i + ") \n");
// Add odd factor
sum += i;
}
i += 1;
}
// Display calculated result
print(" Odd Factors Sum : " + sum + "\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: Factorization = Factorization();
// Test Cases
task.oddFactorSum(12);
task.oddFactorSum(50);
task.oddFactorSum(40);
}
Output
Odd Factors pair of a number 12 is
(1 X 12)
(3 X 4)
Odd Factors Sum : 4
Odd Factors pair of a number 50 is
(1 X 50)
(5 X 10)
(25 X 2)
Odd Factors Sum : 31
Odd Factors pair of a number 40 is
(1 X 40)
(5 X 8)
Odd Factors Sum : 6
Output Explanation
- For the test case oddFactorSum(12), the output shows the odd factor pairs: (1 X 12) and (3 X 4). The sum of the odd factors is 4.
- For the test case oddFactorSum(50), the output shows the odd factor pairs: (1 X 50), (5 X 10), and (25 X 2). The sum of the odd factors is 31.
- For the test case oddFactorSum(40), the output shows the odd factor pair: (1 X 40) and (5 X 8). The sum of the odd factors is 6.
Time Complexity
The time complexity of the provided code is O(n/2) = O(n), where 'n' is the given number. The function oddFactorSum iterates through numbers from 1 to num/2 and performs constant time operations inside the loop. Since we ignore constant factors in Big O notation, the time complexity is O(n).
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment