Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active October 27, 2024 20:12
Show Gist options
  • Save juanfal/e235970d086c69b1d87db0a37c02c895 to your computer and use it in GitHub Desktop.
Save juanfal/e235970d086c69b1d87db0a37c02c895 to your computer and use it in GitHub Desktop.
amicable numbers
// t04e04.amicable.cpp
// juanfc 2024-10-23
// amicable numbers
// 284 220
// 1210 1184
// 2924 2620
// 5564 5020
// 6368 6232
// https://gist.github.com/juanfal/e235970d086c69b1d87db0a37c02c895
#include <iostream>
using namespace std;
int main()
{
void amicLessThan(int n);
void printRes(int a, int b);
printRes(284, 220);
printRes(121, 212);
amicLessThan(3000);
return 0;
}
bool amicable(int a, int b)
{
int addDivisors(int n);
return addDivisors(a) == b and
addDivisors(b) == a;
}
void amicLessThan(int n)
{
bool amicable(int a, int b);
int addDivisors(int n);
for (int i = 1; i <= n; ++i)
for (int j = 0; j < i; ++j)
if (amicable(i, j))
cout << i << " " << j << endl;
}
int addDivisors(int n)
{
int s = 0;
for (int i = 1; i < n; ++i)
if (n % i == 0)
s += i;
return s;
}
void printRes(int a, int b)
{
bool amicable(int a, int b);
if (amicable(a, b))
cout << "The numbers " << a << " and " << b << " ARE amicable" << endl;
else
cout << "The numbers " << a << " and " << b << " are NOT amicable" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment