Last active
March 14, 2017 13:23
-
-
Save creaktive/b1767bdcc6a453e276d4cf073f34666c to your computer and use it in GitHub Desktop.
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> | |
#include <set> | |
#include <math.h> | |
int n_divisors(int n) { | |
int l = sqrt(n); | |
std::set<int> f; | |
f.insert(n); | |
for (int i = 1; i <= l; i++) | |
if (n % i == 0) { | |
f.insert(i); | |
f.insert(n / i); | |
} | |
return f.size(); | |
} | |
int main() { | |
int triangular = 0; | |
for (int i = 1;; i++) { | |
triangular += i; | |
if (n_divisors(triangular) > 500) { | |
std::cout << triangular << std::endl; | |
return 0; | |
} | |
} | |
return 0; | |
} |
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
#!/usr/bin/env perl | |
use 5.018; | |
use strict; | |
use warnings; | |
sub divisors { | |
my ($n) = @_; | |
my %f = ($n => 1); | |
for my $i (1 .. int(sqrt($n))) { | |
unless ($n % $i) { | |
$f{$i}++; | |
$f{int($n / $i)}++; | |
} | |
} | |
return [keys %f]; | |
} | |
my $triangular = 0; | |
for (my $i = 1; ; $i++) { | |
$triangular += $i; | |
my @divisors = @{ divisors($triangular) }; | |
if ($#divisors >= 500) { | |
say $triangular; | |
last; | |
} | |
} |
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
import math | |
def divisors(n): | |
f = set([n]) | |
for i in xrange(1, int(math.sqrt(n)) + 1): | |
if (n % i == 0): | |
f.add(i) | |
f.add(int(n / i)) | |
return f | |
triangular = 0 | |
i = 1 | |
while True: | |
triangular += i | |
if len(divisors(triangular)) > 500: | |
print triangular | |
break | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment