D
D
dmitrii20042021-07-12 13:35:31
C++ / C#
dmitrii2004, 2021-07-12 13:35:31

Find the sum and number of divisors of a natural number?

Numeric functions
The number of all natural divisors of a natural number n is denoted by σ0(n). The sum of all natural divisors of a number n is denoted by σ1(n).

Specifications Input

Given natural number n≤109.

Output

Output σ0(n) and σ1(n).

Note

This problem is recommended to be solved by enumeration of all divisors of a number up to n−−√.

Examples
Input
6
Output
4 12

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
  int n,c=0,s=0;
  cin >> n;
  for (int i = 1; i <= sqrt(n) ; i++)
  {
    if (n % i == 0 and i != sqrt(n)) {
      c += 2;
  
      s += (i + n / i);
      
    }
    if (i == sqrt(n) and n % i == 0)
    {

      c += 1;
      s += i;
      
    }
  }
  cout << c << " " << s;
  
}
Помогите пожалуйста. Сайт пишет что программа выдаёт неверный ответ

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-07-12
@dmitrii2004

Is there an and operator in C++? Try replacing sqrt with checking i*i ==n. Apparently, problems with accuracy. Plus there can be an overflow. The amount must be long long.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question