+ 1

Given a natural number N. FIND the sum of all its divisors that are Prime Number. How Will this be resolved?

5th Dec 2018, 2:28 PM
Настя S
Настя S - avatar
7 Réponses
+ 2
Step 1: Make a function that tells you whether a number is prime or not. Step 2: Iterate over all possible divisors of N (1 to N / 2 and N). Step 3: For each possible divisor, test whether it actually divides N. If it does, test if the number is prime using your function. If the number is prime, add it to a variable that holds the sum of all those numbers. Step 5: Print the result. That's for the theory. If you need help with the actual code, please provide an own attempt of yours first, so we can see that you actually tried solving the problem and don't feel like doing your homework for you :).
5th Dec 2018, 2:36 PM
Shadow
Shadow - avatar
+ 2
There are a few errors in that code. For example, you shouldn't use those ascii symbols with cout and cin, but rather the actual << and >> instead. Also, the prime testing was missing. Here is an example code of how I'd do it: https://code.sololearn.com/c09TtK3MQbsl/?ref=app
5th Dec 2018, 3:14 PM
Shadow
Shadow - avatar
+ 2
A bool is a very simple data type that is either true or false. Either a number is prime or not. https://www.sololearn.com/learn/CPlusPlus/1623/ 'continue' is a keywork that immediately lets the loop jump to its next iteration. And about the prime numbers, why do you need 1? It is not a prime number.
5th Dec 2018, 3:24 PM
Shadow
Shadow - avatar
+ 1
#include<iostream> #include<cmath> #include<stdio.h> using namespace std; int main() { setlocale(LC_ALL, "Rus"); int i, N ,sum = 0; cout « "Введите N: "; cin » N; for (i = 1 ; i <= N ; i++) if (N%i==0) sum = sum+i; cout « sum « endl ; return 0; } how to make the dividers were simple number?
5th Dec 2018, 2:38 PM
Настя S
Настя S - avatar
+ 1
me without functions need
5th Dec 2018, 2:40 PM
Настя S
Настя S - avatar
+ 1
wait, first we did not have (bool ,continue ),second divisors must be Prime numbers (but when I enter 4,it turns out 2, and you need 1):(
5th Dec 2018, 3:21 PM
Настя S
Настя S - avatar
+ 1
OK, Thanks a lot:)
5th Dec 2018, 3:32 PM
Настя S
Настя S - avatar