miércoles, 19 de octubre de 2011

Numeros Amigos

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#define db(a) cout << #a << " = " << a << endl;
using namespace std;
int amigo(int n) {
 int m = 0;
 for (int i = 1; i * i <= n; i++) {
  if(n % i == 0)
   m += i + n / i;
  if(i * i == n) m -= i;
 }
 m -= n;
 int t = 0;
 for (int i = 1; i * i <= m; i++) {
  if(m % i == 0)
   t += i + m / i;
  if(i * i == m) t -= i;
 }
 t -= m;
 if(t == n) return m;
 return -1;
}
int main() {
 int n;
 ios_base::sync_with_stdio(false);
 while(cin >> n && n != 0) {
  cout << amigo(n) << endl;
 }
 return 0;
}



algorithm to determine if a number has a friend number, if not then it shows you -1 instead.

No hay comentarios:

Publicar un comentario