Optimierung für große Zahlen
This commit is contained in:
parent
86dab66e54
commit
ec0238ccc6
2 changed files with 3 additions and 45 deletions
|
@ -27,14 +27,7 @@ int main()
|
|||
|
||||
amount -= 219;
|
||||
|
||||
//Wenn amount oberhalb von 500000 ist, würde das Programm beim Erstellen des Arrays abstürzen.
|
||||
//Dies lässt sich lösen indem man kein Array anlegt,
|
||||
//sondern die Teilersumme jeder Teilersumme von Zahlen ab 220 darauf überprüft ob sie mit der jeweiligen Zahl identisch sind.
|
||||
//Das Programm dazu existiert bereits unte dem Namen "befreundete Zahlen 2".
|
||||
|
||||
if(amount>500000) amount = 500000;
|
||||
|
||||
unsigned int number[amount];
|
||||
unsigned int *number = new unsigned int[amount];
|
||||
|
||||
for(unsigned long long idx = 0; idx<amount; idx++)
|
||||
{
|
||||
|
@ -45,6 +38,8 @@ int main()
|
|||
if(number[idx]<idx+220 && number[idx]>=220 && number[number[idx]-220]==idx+220) std::cout << number[idx] << " und " << number[number[idx]-220] << "\n";
|
||||
}
|
||||
|
||||
delete[] number;
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
#include<iostream>
|
||||
#include<string>
|
||||
#include<sstream>
|
||||
#include<conio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string input;
|
||||
unsigned long long max = 1;
|
||||
|
||||
std::cout << "Gib an bis zu welcher Zahl nach befreundeten Zahlen gesucht werden soll: ";
|
||||
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> max;
|
||||
|
||||
if(max<284)
|
||||
{
|
||||
std::cout << "\nEs wurden keine befreundeten Zahlen gefunden.";
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
std::cout << "\nbefreundete Zahlen sind:\n";
|
||||
|
||||
for(unsigned long long number = 284; number<=max; number++)
|
||||
{
|
||||
unsigned long long sum = 0, sum2 = 0;
|
||||
|
||||
for(unsigned long long factor = 1; factor*2<=number; factor++) if(number%factor==0) sum += factor;
|
||||
|
||||
if(sum<number && sum>=220) for(unsigned long long factor = 1; factor*2<=sum; factor++) if(sum%factor==0) sum2 += factor;
|
||||
if(sum2==number) std::cout << sum << " und " << sum2 << "\n";
|
||||
}
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
Reference in a new issue