Stein
0
Zu erst habe ich das ganze in Perl versucht:Problem 12
08 March 2002
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
Code:
#!/usr/local/bin/perl -w -l
$rekord = 0;
$dieserunde = 0;
for($zahl = 530; $zahl < 539;$zahl++){
$dieserunde = teiler();
if($dieserunde > $rekord){
$rekord= $dieserunde;
print dreieckszahl(), " hat ", $rekord, " Teiler";
}
print $zahl;
}
print $rekord;
sub teiler{
$teileranzahl = 0;
for($j = 1; $j <= dreieckszahl();$j++){
if(dreieckszahl()%$j == 0) {
$teileranzahl++;
}
}
return $teileranzahl;
}
sub dreieckszahl{
$counter = 0;
for($bla = 1; $bla <= $zahl; $bla++){
$counter = $counter +$bla
}
return $counter;
}
Das war aber zulangsam(bei 500 schon pro zahl 20 sec und da hat man gerade mal 48 Teiler) und weil ich erfahren habe, das C++ wesentlich schneller ist:
Code:
#include <iostream>
#include <ostream>
using namespace std;
static int drzahl(int zahl)
{
int counter = 0;
int bla;
for(bla = 1; bla <= zahl; bla++){
counter = counter + bla;
}
return counter;
}
static int teiler(int zahl)
{
int teileranzahl = 0;
int j;
for(j = 1; j <= drzahl(zahl); j++){
if(drzahl(zahl)%j == 0){
teileranzahl++;
}
}
return teileranzahl;
}
int main()
{
int rekord = 0;
int dieserunde = 0;
int i;
for(i = 500; rekord < 500;i++){
dieserunde = teiler(i);
if(dieserunde > rekord){
rekord = dieserunde;
cout << drzahl(i) << " hat " << rekord << " Teiler" << "\n";
}
cout << i << "\n";
}
cout << rekord << "\n";
}
Dann war ich 5h weg und sehe das: 2162160 hat 320 Teiler hab ich so umständlich programmiert oder ist die Aufgabe in einer anständigen Zeit nicht lösbar?