The program is simple. The user inputs n and n amount of numbers and i try to add zeros in between adjacent digits. For example if the user enters 9(n) digits as 1 2 3 4 5 6 7 8 9 (spaced out), the program outputs 10203040506070809. The program works well for up to n=8 digits but i get funny answers from n=9 digits upwards. The range of n should be 3<=n<=15 . My program is as follows:
int main()
{
cout << "\nEnter n and n values: \n";
int n;
cin >> n;
vector<long long>nums;
int en = n;
while (en > 0)
{
long long x;
cin >> x;
nums.push_back(x);
--en;
}
int r = 2 * n - 2;
long long new_val = 0;
int j = 0;
for (int i = 0; i < n; ++i)
{
new_val = new_val + nums[i] * (pow(10, r - j));
j += 2;
}
cout << new_val << endl;
}
I don't know how to solve the issue of funny answers from n=9 to n=15.
The main problem is long long is only 64-bits in size, and thus can only hold up to 19 digits. Its maximum value is 9,223,372,036,854,775,807.
It is possible to make your code work correctly up to n=10 if you simply remove pow() (which operates on floating-point types, not integer types). For the 2nd and subsequent loop iterations, you can simply multiply new_val by 100 before adding nums[i]:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout << "\nEnter n and n values: \n";
int n;
cin >> n;
vector<long long> nums;
int en = n;
while (en > 0)
{
long long x;
cin >> x;
nums.push_back(x);
--en;
}
long long new_val = 0;
if (n > 0)
{
new_val = nums[0];
for (int i = 1; i < n; ++i)
{
new_val *= 100;
new_val += nums[i];
}
}
cout << new_val << endl;
}
Live Demo
However, 1020304050607080901 is 19 digits, so n>=11 will overflow past the max value of long long.
Live Demo
For such high values, you need to use a BigNumber library (as most compilers do not yet have a native 128-bit numeric type). Or, just use std::string instead of long long:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{
cout << "\nEnter n and n values: \n";
int n;
cin >> n;
vector<int> nums;
int en = n;
while (en > 0)
{
int x;
cin >> x;
nums.push_back(x);
--en;
}
ostringstream new_val;
if (n > 0)
{
new_val << nums[0];
for (int i = 1; i < n; ++i)
new_val << '0' << nums[i];
}
cout << new_val.str() << endl;
}
Live Demo
Allow me to suggest a different approach to this problem. Since containers will give you trouble holding those large numbers, then don't with them as numbers, try string instead:
After you get the numbers from the user and store them in the nums vector, do the following:
string output = "";
char temp;
for (int i = 0; i < nums.size(); i++){
temp = nums [i] + '0';
output += temp;
if (i != (nums.size()-1))
output += "0";
}
cout << output;
Using this method, the user can enter any number as large as they want. If you want the number to be from 9 to 15, you can simply add validation at the beginning without having to deal with complicated containers.
In ProjectEuler problem #14, one needs to find the longest Collatz chain, up to 1 million. I found a halfway decent way to do so, however, it feels like I'm just being stupid because I can't find a way to make this code more efficient (the code is supposed to only print out the solution, after it tests 1 to 1 million, but hasn't printed anyting out after 10 minutes). Am I tackling this problem the wrong way, or is there a way to optimize my existing code?
#include <iostream>
using namespace std;
int main()
{
int i;
int x;
int n;
int superN;
int superI;
superN = 0;
superI = 0;
for (i = 1; i <= 1000000; i++) {
x = i;
n = 1;
do {
if (x % 2 == 0) {
x = x / 2;
}
if (x % 2 == 1 && x != 1) {
x = 3 * x + 1;
}
n++;
if (n > superN) {
superN = n;
superI = i;
}
} while (x != 1);
}
cout << "The number " << superI << " ran for " << superN << " terms.";
system("pause");
return 0;
}
You've got a few small problems:
I'm fairly sure that you are overflowing the int data type. Use a uint64_t to make this far less likely.
You should only update superI and superN outside of the while loop. This shouldn't matter, but it hurts performance.
On each iteration you should only modify x once. You currently might modify it twice, which might cause you to fall into an infinite loop. And your calculation of n will be off as well.
Use memoization to improve performance by caching old results.
Applying this, you could come up with some code like this:
#include <cstdint>
#include <iostream>
#include <map>
using namespace std;
int main()
{
uint64_t i;
uint64_t x;
uint64_t n;
uint64_t superN;
uint64_t superI;
std::map<uint64_t, uint64_t> memory;
superN = 0;
superI = 0;
for (i = 1; i <= 1000000; i++) {
x = i;
n = 1;
do {
if (memory.find(x) != memory.end()) {
n += memory[x];
break;
}
if (x % 2 == 0) {
x = x / 2;
} else {
x = 3 * x + 1;
}
n++;
} while (x != 1);
if (n > superN) {
superN = n;
superI = i;
}
memory[i] = n;
}
cout << "The number " << superI << " ran for " << superN << " terms.\n";
system("pause");
return 0;
}
Which takes 4 seconds to output:
The number 837799 ran for 556 terms.
I would suggest not to use memoization as for me it run slower; in my case (up to 10,000,000) the code below is faster without memoization.
the main changes are:
only testing if the current number is even once (not need for an else-if).
using a bitwise operation instead of the modulo operation (slightly faster)
Apart from that I don't know why your code is so long (mine is below 200 milliseconds) maybe you compile as debug ?
bool isEven(uint64_t value)
{
return (!(value & 1));
}
uint64_t solveCollatz(uint64_t start)
{
uint64_t counter = 0;
while (start != 1)
{
if(isEven(start))
{
start /= 2;
}
else
{
start = (3 * start) + 1;
}
counter++;
}
return counter;
}
If you can use compiler intrinsics, particularly with counting and removing trailing zeros, you'll recognize you don't need to branch in the main loop, you'll always alternate odd and even. The memoization techniques that have been previously presented will rarely short-circuit around the math you're doing, since we're dealing with hailstone numbers - additionally, the majority of numbers only have one entry point, so if you see them once, you'll never see them again.
In GCC it'll look something like this:
#include <cstdint>
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;
using n_iPair = std::pair<uint32_t, uint64_t>;
auto custComp = [](n_iPair a, n_iPair b){
return a.first < b.first;
};
int main()
{
uint64_t x;
uint64_t n;
n_iPair Super = {0,0};
for (auto i = 1; i <= 1000000; i++){
x = i;
n = 0;
if (x % 2 == 0) {
n += __builtin_ctz(x); // account for all evens
x >>= __builtin_ctz(x); // always returns an odd
}
do{ //when we enter we're always working on an odd number
x = 3 * x + 1; // always increases an odd to an even
n += __builtin_ctz(x)+1; // account for both odd and even transfer
x >>= __builtin_ctz(x); // always returns odd
}while (x != 1);
Super = max(Super, {n,i}, custComp);
}
cout << "The number " << Super.second << " ran for " << Super.first << " terms.\n";
return 0;
}
If performance is critical, but memory isn't, you can use caching to improve the speed.
#include <iostream>
#include <chrono>
#include <vector>
#include <sstream>
std::pair<uint32_t, uint32_t> longestCollatz(std::vector<uint64_t> &cache)
{
uint64_t length = 0;
uint64_t number = 0;
for (uint64_t current = 2; current < cache.size(); current++)
{
uint64_t collatz = current;
uint64_t steps = 0;
while (collatz != 1 && collatz >= current)
{
if (collatz % 2)
{
// if a number is odd, then ((collatz * 3) + 1) would result in
// even number, but even number can have even or odd result, so
// we can combine two steps for even number, and increment twice.
collatz = ((collatz * 3) + 1) / 2;
steps += 2;
}
else
{
collatz = collatz / 2;
steps++;
}
}
cache[current] = steps + cache[collatz];
if (cache[current] > length)
{
length = cache[current];
number = current;
}
}
return std::make_pair(number, length);
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();;
uint64_t input = 1000000;
std::vector<uint64_t> cache(input + 1);
auto longest = longestCollatz(cache);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Longest Collatz (index : value) --> " << longest.first << " : " << longest.second;
std::cout << "\nExecution time: " << duration << " milliseconds\n";
return EXIT_SUCCESS;
}
I am trying to calculate two pentagonal numbers whose sum and difference will produce another pentagonal number. In my main function I use pentagonal number theorem to produce pentagonal number sums that produce a pentagonal number and then I check if the difference of those 2 numbers is also pentagonal using is_pentagonal function.
I've written the following code in C++ and for some reason in doesn't give the correct answer and I'm not sure where the mistake is.
The thing is, when I get my answer d then j and k are not pentagonal. j and k simply go above the numerical limit and random numbers eventually produce pentagonal d and i don't get why it happens. Thank you.
bool is_perfect_square(int n)
{
if (n < 0) return false;
int root = sqrt(n);
return n == root * root;
}
bool is_pentagonal(int n)
{
if(is_perfect_square(24*n + 1) && (int)sqrt(24*n+1)%6 == 5)return true;
return false;
}
int main() {
int j = 0, k = 0, d = 0, n = 1;
while(!is_pentagonal(d))
{
j = (3*n+1)*(3*(3*n+1)-1)/2;
k = (n*(9*n+5)/2)*(3*n*(9*n+5)/2-1)/2;
d = k - j;
++n;
}
cout << d << endl;
return 0;
}
I've run this code in ideone:
#include <iostream>
#include <math.h>
using namespace std;
bool is_perfect_square(unsigned long long int n);
bool is_pentagonal(unsigned long long int n);
int main() {
// I was just verifying that your functions are correct
/*
for (int i=0; i<100; i++) {
cout << "Number " << i << " is pentagonal? " << is_pentagonal(i) << endl;
}
*/
unsigned long long int j = 0, k = 0, d = 0;
int n = 1;
while(!is_pentagonal(d))
{
j = (3*n+1)*(3*(3*n+1)-1)/2;
if (!is_pentagonal(j)) {
cout << "Number j = " << j << " is not pentagonal; n = " << n << endl;
}
k = (n*(9*n+5)/2)*(3 *n*(9*n+5)/2-1)/2;
if (!is_pentagonal(k)) {
cout << "Number k = " << k << " is not pentagonal; n = " << n << endl;
}
d = k - j;
++n;
}
cout << "D = |k-j| = " << d << endl;
return 0;
}
bool is_perfect_square(unsigned long long int n) {
if (n < 0)
return false;
unsigned long long int root = sqrt(n);
return n == root * root;
}
bool is_pentagonal(unsigned long long int n)
{
if(is_perfect_square(24*n + 1) && (1+(unsigned long long int)sqrt(24*n+1))%6 == 0)return true;
return false;
}
And the result is:
Number k = 18446744072645291725 is not pentagonal; n = 77
Number k = 18446744072702459675 is not pentagonal; n = 78
Number k = 18446744072761861113 is not pentagonal; n = 79
...
If you compare these numbers with 2^64 = 18 446 744 073 709 551 616 as reported at cplusplus.com you will notice you are very close to that. So basically what happens is that your algorithm is correct, but numbers quickly get so big they overflow, and then they are just wrong. See here to check what options you have now.
I have this problem in making a program that helps me with this.
For n (n <= 25). Make a program that calculates and shows on the screen the value of the sum:
S= 1+ 2+ 2(pow 2)+ 2(pow 3)+...+2(pow n).
what i managed to do is this :
#include <iostream>
#include <math.h>
using namespace std;
int i;
int n;
long s;
long f() {
if (n=0) {
return 1;
}else if (n=1) {
return 2;
}else {
return 2* (n-1);
}
}
int main() {
for (i=0; n<=2;++n){
s=s+f();
cout << s <<endl;
}
}
The main code is wrong i know that for sure but i do not know how to do it..please help me, im just a c++ begginer and trying to learn the language on my own.
The specific things you're doing wrong...
int i;
int n;
long s;
Don't use globals like this. You should need no globals at all for this program.
long f() {
if (n=0) {
return 1;
}else if (n=1) {
return 2;
}else {
return 2* (n-1);
}
}
Here you're using recursion where you should use a loop instead. Also, n should be a passed-in parameter:
long f(int n) {
long result = 1;
for(int i = 0; i < n; ++i)
result *= 2;
return result;
}
Or even better, don't reinvent the wheel and use pow(2, n) instead of f(n).
for (i=0; n<=2;++n){
You set i but never do anything with it.
You never initialize n or s so they could have random values (though these days compilers are nicer to people and set all the uninitialized globals to 0, but you really shouldn't depend on that).
Ergo, you should have written n=0 instead of i=0.
How it could have looked if you didn't use globals:
int main() {
long s = 0;
for (int n = 0; n <= 2; ++n){
s += f(n);
cout << s <<endl;
}
}
This is just a geometric series. Sum of n terms of geometric series is given by:-
S(n) = a ( r^n - 1 )/ (r - 1 )
n = no. of terms.
r = common ratio.
a = first term.
So, for your example...
a = 1.
r = 2.
n = no of terms you want to take sum.
2(pow n) may be written 1 << n
or if you want to compute yourself the power of two:
// compute manually (1 << n)
int power2(int n)
{
int res = 1;
for (int i = 0; i != n; ++i) {
res *= 2
}
return res;
}
Your sum is in fact power2(n+1) - 1, so you may simply write:
std::cout << ((1 << n + 1) - 1) << std::endl;
or
std::cout << power2(n + 1) - 1 << std::endl;
if you want to do that in loop:
unsigned int res = 0;
for (int i = 0; i != n; ++i) {
res += power2(i);
}
std::cout << res << std::endl;
All you need is a variable to hold the current sum and another variable to hold the power of 2:
int main()
{
const int n = 25;
int pow2 = 1;
int sum = 1;
for (int i = 1; i <= n; i++)
{
pow2 *= 2;
sum += pow2;
}
cout << sum << endl;
}
According to this post, we can get all divisors of a number through the following codes.
for (int i = 1; i <= num; ++i){
if (num % i == 0)
cout << i << endl;
}
For example, the divisors of number 24 are 1 2 3 4 6 8 12 24.
After searching some related posts, I did not find any good solutions. Is there any efficient way to accomplish this?
My solution:
Find all prime factors of the given number through this solution.
Get all possible combinations of those prime factors.
However, it doesn't seem to be a good one.
Factors are paired. 1 and 24, 2 and 12, 3 and 8, 4 and 6.
An improvement of your algorithm could be to iterate to the square root of num instead of all the way to num, and then calculate the paired factors using num / i.
You should really check till square root of num as sqrt(num) * sqrt(num) = num:
Something on these lines:
int square_root = (int) sqrt(num) + 1;
for (int i = 1; i < square_root; i++) {
if (num % i == 0&&i*i!=num)
cout << i << num/i << endl;
if (num % i == 0&&i*i==num)
cout << i << '\n';
}
There is no efficient way in the sense of algorithmic complexity (an algorithm with polynomial complexity) known in science by now. So iterating until the square root as already suggested is mostly as good as you can be.
Mainly because of this, a large part of the currently used cryptography is based on the assumption that it is very time consuming to compute a prime factorization of any given integer.
Here's my code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define pii pair<int, int>
#define MAX 46656
#define LMT 216
#define LEN 4830
#define RNG 100032
unsigned base[MAX / 64], segment[RNG / 64], primes[LEN];
#define sq(x) ((x)*(x))
#define mset(x,v) memset(x,v,sizeof(x))
#define chkC(x,n) (x[n>>6]&(1<<((n>>1)&31)))
#define setC(x,n) (x[n>>6]|=(1<<((n>>1)&31)))
// http://zobayer.blogspot.com/2009/09/segmented-sieve.html
void sieve()
{
unsigned i, j, k;
for (i = 3; i<LMT; i += 2)
if (!chkC(base, i))
for (j = i*i, k = i << 1; j<MAX; j += k)
setC(base, j);
primes[0] = 2;
for (i = 3, j = 1; i<MAX; i += 2)
if (!chkC(base, i))
primes[j++] = i;
}
//http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/
vector <pii> factors;
void primeFactors(int num)
{
int expo = 0;
for (int i = 0; primes[i] <= sqrt(num); i++)
{
expo = 0;
int prime = primes[i];
while (num % prime == 0){
expo++;
num = num / prime;
}
if (expo>0)
factors.push_back(make_pair(prime, expo));
}
if ( num >= 2)
factors.push_back(make_pair(num, 1));
}
vector <int> divisors;
void setDivisors(int n, int i) {
int j, x, k;
for (j = i; j<factors.size(); j++) {
x = factors[j].first * n;
for (k = 0; k<factors[j].second; k++) {
divisors.push_back(x);
setDivisors(x, j + 1);
x *= factors[j].first;
}
}
}
int main() {
sieve();
int n, x, i;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
primeFactors(x);
setDivisors(1, 0);
divisors.push_back(1);
sort(divisors.begin(), divisors.end());
cout << divisors.size() << "\n";
for (int j = 0; j < divisors.size(); j++) {
cout << divisors[j] << " ";
}
cout << "\n";
divisors.clear();
factors.clear();
}
}
The first part, sieve() is used to find the prime numbers and put them in primes[] array. Follow the link to find more about that code (bitwise sieve).
The second part primeFactors(x) takes an integer (x) as input and finds out its prime factors and corresponding exponent, and puts them in vector factors[]. For example, primeFactors(12) will populate factors[] in this way:
factors[0].first=2, factors[0].second=2
factors[1].first=3, factors[1].second=1
as 12 = 2^2 * 3^1
The third part setDivisors() recursively calls itself to calculate all the divisors of x, using the vector factors[] and puts them in vector divisors[].
It can calculate divisors of any number which fits in int. Also it is quite fast.
Plenty of good solutions exist for finding all the prime factors of not too large numbers. I just wanted to point out, that once you have them, no computation is required to get all the factors.
if N = p_1^{a}*p_{2}^{b}*p_{3}^{c}.....
Then the number of factors is clearly (a+1)(b+1)(c+1).... since every factor can occur zero up to a times.
e.g. 12 = 2^2*3^1 so it has 3*2 = 6 factors. 1,2,3,4,6,12
======
I originally thought that you just wanted the number of distinct factors. But the same logic applies. You just iterate over the set of numbers corresponding to the possible combinations of exponents.
so int he example above:
00
01
10
11
20
21
gives you the 6 factors.
If you want all divisors to be printed in sorted order
int i;
for(i=1;i*i<n;i++){ /*print all the divisors from 1(inclusive) to
if(n%i==0){ √n (exclusive) */
cout<<i<<" ";
}
}
for( ;i>=1;i--){ /*print all the divisors from √n(inclusive) to
if(n%i==0){ n (inclusive)*/
cout<<(n/i)<<" ";
}
}
If divisors can be printed in any order
for(int j=1;j*j<=n;j++){
if(n%j==0){
cout<<j<<" ";
if(j!=(n/j))
cout<<(n/j)<<" ";
}
}
Both approaches have complexity O(√n)
Here is the Java Implementation of this approach:
public static int countAllFactors(int num)
{
TreeSet<Integer> tree_set = new TreeSet<Integer>();
for (int i = 1; i * i <= num; i+=1)
{
if (num % i == 0)
{
tree_set.add(i);
tree_set.add(num / i);
}
}
System.out.print(tree_set);
return tree_set.size();
}
//Try this,it can find divisors of verrrrrrrrrry big numbers (pretty efficiently :-))
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<conio.h>
using namespace std;
vector<double> D;
void divs(double N);
double mod(double &n1, double &n2);
void push(double N);
void show();
int main()
{
double N;
cout << "\n Enter number: "; cin >> N;
divs(N); // find and push divisors to D
cout << "\n Divisors of "<<N<<": "; show(); // show contents of D (all divisors of N)
_getch(); // used visual studio, if it isn't supported replace it by "getch();"
return(0);
}
void divs(double N)
{
for (double i = 1; i <= sqrt(N); ++i)
{
if (!mod(N, i)) { push(i); if(i*i!=N) push(N / i); }
}
}
double mod(double &n1, double &n2)
{
return(((n1/n2)-floor(n1/n2))*n2);
}
void push(double N)
{
double s = 1, e = D.size(), m = floor((s + e) / 2);
while (s <= e)
{
if (N==D[m-1]) { return; }
else if (N > D[m-1]) { s = m + 1; }
else { e = m - 1; }
m = floor((s + e) / 2);
}
D.insert(D.begin() + m, N);
}
void show()
{
for (double i = 0; i < D.size(); ++i) cout << D[i] << " ";
}
int result_num;
bool flag;
cout << "Number Divisors\n";
for (int number = 1; number <= 35; number++)
{
flag = false;
cout << setw(3) << number << setw(14);
for (int i = 1; i <= number; i++)
{
result_num = number % i;
if (result_num == 0 && flag == true)
{
cout << "," << i;
}
if (result_num == 0 && flag == false)
{
cout << i;
}
flag = true;
}
cout << endl;
}
cout << "Press enter to continue.....";
cin.ignore();
return 0;
}
for (int i = 1; i*i <= num; ++i)
{
if (num % i == 0)
cout << i << endl;
if (num/i!=i)
cout << num/i << endl;
}
for( int i = 1; i * i <= num; i++ )
{
/* upto sqrt is because every divisor after sqrt
is also found when the number is divided by i.
EXAMPLE like if number is 90 when it is divided by 5
then you can also see that 90/5 = 18
where 18 also divides the number.
But when number is a perfect square
then num / i == i therefore only i is the factor
*/
//DIVISORS IN TIME COMPLEXITY sqrt(n)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll int n;
cin >> n;
for(ll i = 2; i <= sqrt(n); i++)
{
if (n%i==0)
{
if (n/i!=i)
cout << i << endl << n/i<< endl;
else
cout << i << endl;
}
}
}
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD 1000000007
#define fo(i,k,n) for(int i=k;i<=n;++i)
#define endl '\n'
ll etf[1000001];
ll spf[1000001];
void sieve(){
ll i,j;
for(i=0;i<=1000000;i++) {etf[i]=i;spf[i]=i;}
for(i=2;i<=1000000;i++){
if(etf[i]==i){
for(j=i;j<=1000000;j+=i){
etf[j]/=i;
etf[j]*=(i-1);
if(spf[j]==j)spf[j]=i;
}
}
}
}
void primefacto(ll n,vector<pair<ll,ll>>& vec){
ll lastprime = 1,k=0;
while(n>1){
if(lastprime!=spf[n])vec.push_back(make_pair(spf[n],0));
vec[vec.size()-1].second++;
lastprime=spf[n];
n/=spf[n];
}
}
void divisors(vector<pair<ll,ll>>& vec,ll idx,vector<ll>& divs,ll num){
if(idx==vec.size()){
divs.push_back(num);
return;
}
for(ll i=0;i<=vec[idx].second;i++){
divisors(vec,idx+1,divs,num*pow(vec[idx].first,i));
}
}
void solve(){
ll n;
cin>>n;
vector<pair<ll,ll>> vec;
primefacto(n,vec);
vector<ll> divs;
divisors(vec,0,divs,1);
for(auto it=divs.begin();it!=divs.end();it++){
cout<<*it<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
sieve();
ll t;cin>>t;
while(t--) solve();
return 0;
}
We can use modified sieve for getting all the factors for all numbers in range [1, N-1].
for (int i = 1; i < N; i++) {
for (int j = i; j < N; j += i) {
ans[j].push_back(i);
}
}
The time complexity is O(N * log(N)) as the sum of harmonic series 1 + 1/2 + 1/3 + ... + 1/N can be approximated to log(N).
More info about time complexity : https://math.stackexchange.com/a/3367064
P.S : Usually in programming problems, the task will include several queries where each query represents a different number and hence precalculating the divisors for all numbers in a range at once would be beneficial as the lookup takes O(1) time in that case.
java 8 recursive (works on HackerRank). This method includes option to sum and return the factors as an integer.
static class Calculator implements AdvancedArithmetic {
public int divisorSum(int n) {
if (n == 1)
return 1;
Set<Integer> set = new HashSet<>();
return divisorSum( n, set, 1);
}
private int divisorSum(int n, Set<Integer> sum, int start){
if ( start > n/2 )
return 0;
if (n%start == 0)
sum.add(start);
start++;
divisorSum(n, sum, start);
int total = 0;
for(int number: sum)
total+=number;
return total +n;
}
}