help in transfoming C++ code into regular ansi C..
the instruction for building this program is not allowing me
to call functions,use iosteam ,use arrays
what they want is just If ,while,for commands , simple variable,only stdio.h
in C
how to transform this code into that??
Code:
#include <iostream>
unsigned sum_of_divisors(unsigned n) {
int result=1;
for(unsigned k=2;k*k<=n;++k) {
if(n%k==0) {
int sum_of_powers=1, power=1;
do {
power*=k;
sum_of_powers+=power;
n/=k;
} while(n%k==0);
result*=sum_of_powers;
}
}
return n>1? (n+1)*result : result;
}
bool is_abundant(unsigned n) {
// The body of this function can be substituted by simply
// return sum_of_divisors(n)>2*n;
// This is a memoized version.
const int cache_size=1000000;
static char cache[cache_size]={0};
if(n<cache_size && cache[n])
return cache[n]-1;
bool result=(sum_of_divisors(n)>2*n);
if(n<cache_size)
cache[n] = 1+result;
return result;
}
void print_if_sum_of_abundants(unsigned n) {
for(unsigned i=1;i*2<=n;++i) {
if(is_abundant(i)&&is_abundant(n-i)) {
std::cout << n << " = " << i << " + " << (n-i) << '\n';
return;
}
}
}
int main() {
int limit;
std::cout << "Enter limit: ";
std::cin >> limit;
for(unsigned i=1;i<=limit;++i)
print_if_sum_of_abundants(i);
}