//Autor: Artur Czekalski (Sator)  www.epokaY.net/artur  artur@epokaY.net
//Generowanie liczb doskonałych
#include "stdafx.h"
#include <math.h>  //sqrt
#include <windows.h> //GetTickCount()
//---------------------------------------------------------------------------
int main(int, char* [])
{
 double s;
 int q, nS, SumaP, Liczba = 2;
 int czas;
 
 printf("Liczby doskonale:\n");
 
 czas = GetTickCount();
 while (++Liczba < 500000)
  {SumaP = 1; //suma podzielników; każda liczba dzieli się przez 1
   s = sqrt((double)Liczba);
   nS = (int)s; //zapamiętuje s w int nS dla szybszego porównywania w pętli
   q = 1; //zaczynam dzielenie od 2
   while (++q <= nS)
    if (Liczba % q == 0) SumaP += q + Liczba/q;
   if (q == s) SumaP += q;

   if (SumaP == Liczba) printf("%d\n", Liczba); //suma podzielników Liczby = Liczba
  }
 czas = GetTickCount() - czas;
 //---
 printf("\nVisual C++ 6.0. Czas=%d", czas);
 getchar();  return 0;
}