March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,21 +1,12 @@
|
|||
import std.stdio, std.math, std.range, std.algorithm;
|
||||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
bool isPerfectNumber(in int n) pure nothrow {
|
||||
if (n < 2)
|
||||
return false;
|
||||
|
||||
int sum = 1;
|
||||
foreach (immutable i; 2 .. cast(int)sqrt(cast(real)n) + 1)
|
||||
if (n % i == 0) {
|
||||
immutable int q = n / i;
|
||||
sum += i;
|
||||
if (q > i)
|
||||
sum += q;
|
||||
}
|
||||
|
||||
return sum == n;
|
||||
bool isPerfectNumber1(in uint n) pure nothrow
|
||||
in {
|
||||
assert(n > 0);
|
||||
} body {
|
||||
return n == iota(1, n - 1).filter!(i => n % i == 0).sum;
|
||||
}
|
||||
|
||||
void main() {
|
||||
10_000.iota.filter!isPerfectNumber.writeln;
|
||||
iota(1, 10_000).filter!isPerfectNumber1.writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,21 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
import std.stdio, std.math, std.range, std.algorithm;
|
||||
|
||||
bool isPerfect(in uint n) pure nothrow
|
||||
in {
|
||||
assert(n > 0);
|
||||
} body {
|
||||
return n == reduce!((s, i) => n % i ? s : s + i)(0, iota(1, n-1));
|
||||
bool isPerfectNumber2(in int n) pure nothrow {
|
||||
if (n < 2)
|
||||
return false;
|
||||
|
||||
int total = 1;
|
||||
foreach (immutable i; 2 .. cast(int)real(n).sqrt + 1)
|
||||
if (n % i == 0) {
|
||||
immutable int q = n / i;
|
||||
total += i;
|
||||
if (q > i)
|
||||
total += q;
|
||||
}
|
||||
|
||||
return total == n;
|
||||
}
|
||||
|
||||
void main() {
|
||||
iota(1, 10_000).filter!isPerfect.writeln;
|
||||
10_000.iota.filter!isPerfectNumber2.writeln;
|
||||
}
|
||||
|
|
|
|||
23
Task/Perfect-numbers/Dart/perfect-numbers-1.dart
Normal file
23
Task/Perfect-numbers/Dart/perfect-numbers-1.dart
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* Function to test if a number is a perfect number
|
||||
* A number is a perfect number if it is equal to the sum of all its divisors
|
||||
* Input: Positive integer n
|
||||
* Output: true if n is a perfect number, false otherwise
|
||||
*/
|
||||
bool isPerfect(int n){
|
||||
//Generate a list of integers in the range 1 to n-1 : [1, 2, ..., n-1]
|
||||
List<int> range = new List<int>.generate(n-1, (int i) => i+1);
|
||||
|
||||
//Create a list that filters the divisors of n from range
|
||||
List<int> divisors = new List.from(range.where((i) => n%i == 0));
|
||||
|
||||
//Sum the all the divisors
|
||||
int sumOfDivisors = 0;
|
||||
for (int i = 0; i < divisors.length; i++){
|
||||
sumOfDivisors = sumOfDivisors + divisors[i];
|
||||
}
|
||||
|
||||
// A number is a perfect number if it is equal to the sum of its divisors
|
||||
// We return the test if n is equal to sumOfDivisors
|
||||
return n == sumOfDivisors;
|
||||
}
|
||||
2
Task/Perfect-numbers/Dart/perfect-numbers-2.dart
Normal file
2
Task/Perfect-numbers/Dart/perfect-numbers-2.dart
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
isPerfect(n) =>
|
||||
n == new List.generate(n-1, (i) => n%(i+1) == 0 ? i+1 : 0).fold(0, (p,n)=>p+n);
|
||||
2
Task/Perfect-numbers/Dart/perfect-numbers-3.dart
Normal file
2
Task/Perfect-numbers/Dart/perfect-numbers-3.dart
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
main() =>
|
||||
new List.generate(1000,(i)=>i+1).where(isPerfect).forEach(print);
|
||||
|
|
@ -1 +1,2 @@
|
|||
perf n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
|
||||
perfect n =
|
||||
n == sum [i | i <- [1..n-1], n `mod` i == 0]
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
def perf(n)
|
||||
sum = 0
|
||||
for i in 1...n
|
||||
if n % i == 0
|
||||
sum += i
|
||||
end
|
||||
end
|
||||
return sum == n
|
||||
sum = 0
|
||||
for i in 1...n
|
||||
sum += i if n % i == 0
|
||||
end
|
||||
sum == n
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
def perf(n)
|
||||
n == (1...n).select {|i| n % i == 0}.inject(:+)
|
||||
n == (1...n).select {|i| n % i == 0}.inject(:+)
|
||||
end
|
||||
|
|
|
|||
7
Task/Perfect-numbers/Ruby/perfect-numbers-3.rb
Normal file
7
Task/Perfect-numbers/Ruby/perfect-numbers-3.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def perf(n)
|
||||
divisors = []
|
||||
for i in 1..Math.sqrt(n)
|
||||
divisors << i << n/i if n % i == 0
|
||||
end
|
||||
divisors.uniq.inject(:+) == 2*n
|
||||
end
|
||||
3
Task/Perfect-numbers/Ruby/perfect-numbers-4.rb
Normal file
3
Task/Perfect-numbers/Ruby/perfect-numbers-4.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
for n in 1..10000
|
||||
puts n if perf(n)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue