Created
December 3, 2009 21:36
-
-
Save pagenoare/248550 to your computer and use it in GitHub Desktop.
Wyszukaj liczby zaprzyjaźnione z przedziału...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macbook $ time ./zaprzyjaznione | |
Podaj przedział: 1500 | |
284 i 220 są liczbami zaprzyjaźnonymi | |
1210 i 1184 są liczbami zaprzyjaźnonymi | |
real 0m4.903s | |
user 0m3.616s | |
sys 0m0.000s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int suma_dzielnikow(int a) | |
{ | |
int suma = 0; | |
int i = 1; | |
int x = a / 2; | |
if(a % 2 == 0) { | |
while(i <= x) | |
{ | |
if(a % i == 0) | |
{ | |
suma = suma + i; | |
} | |
i++; | |
} | |
} else { | |
while(i <= x) | |
{ | |
if(a % i == 0) { | |
suma = suma + i; | |
} | |
i += 2; | |
} | |
} | |
return suma; | |
} | |
bool zaprz(int a, int b) | |
{ | |
int s_a = suma_dzielnikow(a); | |
if(b != s_a) { return false; } | |
int s_b = suma_dzielnikow(b); | |
return (a == s_b); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int n; | |
cout << "Podaj przedział: "; | |
cin >> n; | |
int i = 0; | |
while(i != n) | |
{ | |
for(int j = 0; j < i; j++) | |
{ | |
if(zaprz(i, j)) { | |
cout << i << " i " << j << " są liczbami zaprzyjaźnonymi" << endl; | |
} | |
} | |
i++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment