
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find sum of a number and its maximum prime factor in C++
Suppose we have a positive number n, and we have to find the sum of N and its maximum prime factor. So when the number is 26, then maximum prime factor is 13, so sum will be 26 + 13 = 39.
Approach is straight forward. Simply find the max prime factor, and calculate the sum and return.
Example
#include<iostream> #include<cmath> using namespace std; int maxPrimeFact(int n){ int num = n; int maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n /= 2; } for (int i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } if (n > 2) maxPrime = n; return maxPrime; } int getRes(int n) { int sum = maxPrimeFact(n) + n; return sum; } int main() { int n = 26; cout << "Sum of " << n <<" and its max prime factor is: " << getRes(n); }
Output
Sum of 26 and its max prime factor is: 39
- Related Articles
- Find largest prime factor of a number using C++.
- Python Program for Find largest prime factor of a number
- C Program for Find largest prime factor of a number?
- Java Program to find largest prime factor of a number
- Prime factor array of a Number in JavaScript
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- C Program for Find the largest prime factor of a number?
- k-th prime factor of a given number in java
- Finding the largest prime factor of a number in JavaScript
- Prime digits sum of a number in JavaScript
- Maximum number of unique prime factors in C++
- Program to find maximum difference of any number and its next smaller number in Python
- The sum of a number and its reciprocal is $\frac{17}{4}$. Find the number.
- The sum of a number and its successor is \( 79 . \) Find the successor of the number.
- Even Number With Prime Sum

Advertisements