/* 12: 2 3 4 6 13: 14: 2 7 want a function factors(N) which e.g. factors(12) prints 12: 2 3 4 6 void printall(int from, int to) { if (from <= to) { cout << from << " "; printall(from + 1, to); } } printall(2, 11) would print 2 3 4 5 6 7 8 9 10 11 void printalldivisors(int N, int from, int to) { if (from <= to) { if (N % from == 0) cout << from << " "; printalldivisors(N, from + 1, to); } } printalldivisors(12, 2, 11) would print 2 3 4 6 void printalldivisors(int N, int from) { if (from <= N - 1) { if (N % from == 0) cout << from << " "; printalldivisors(N, from + 1); } } printalldivisors(12, 2) would print 2 3 4 6 now want a function that prints the same things, but it also tells us how many divisors there were. */ #include using namespace std; int count_and_print_all_divisors(int N, int from) { if (from <= N - 1) { if (N % from == 0) { cout << from << " "; int count = count_and_print_all_divisors(N, from + 1); return count + 1; } else { int count = count_and_print_all_divisors(N, from + 1); return count; } } return 0; } void list_divisors(int from, int to) { if (from <= to) { cout << from << ": "; int N = count_and_print_all_divisors(from, 2); if (N == 0) cout << "(prime)"; cout << "\n"; list_divisors(from + 1, to); } } // int main() // { int N = count_and_print_all_divisors(240, 2); // cout << "\nthe number of divisors was " << N << "\n"; } int main() { list_divisors(1, 24); } /* int count_and_print_all_divisors(int N, int from) { int total = 0; for (int i = from; i < N; i += 1) { if (N % i == 0) { cout << i << " "; count += 1; } } return count; } */