September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,48 +1,62 @@
-- PERFECT NUMBERS -----------------------------------------------------------
-- perfect :: integer -> bool
on perfect(n)
-- isFactor :: integer -> bool
script isFactor
on lambda(x)
on |λ|(x)
n mod x = 0
end lambda
end |λ|
end script
-- quotient :: number -> number
script quotient
on lambda(x)
on |λ|(x)
n / x
end lambda
end |λ|
end script
-- sum :: number -> number -> number
script sum
on lambda(a, b)
on |λ|(a, b)
a + b
end lambda
end |λ|
end script
-- Integer factors of n below the square root
set lows to filter(isFactor, range(1, (n ^ (1 / 2)) as integer))
set lows to filter(isFactor, enumFromTo(1, (n ^ (1 / 2)) as integer))
-- low and high factors (quotients of low factors) tested for perfection
(n > 1) and (foldl(sum, 0, (lows & map(quotient, lows))) / 2 = n)
end perfect
-- TEST
-- TEST ----------------------------------------------------------------------
on run
filter(perfect, range(1, 10000))
filter(perfect, enumFromTo(1, 10000))
--> {6, 28, 496, 8128}
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- GENERIC LIBRARY FUNCTIONS
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
@ -51,7 +65,7 @@ on filter(f, xs)
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if lambda(v, i, xs) then set end of lst to v
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
@ -63,7 +77,7 @@ on foldl(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -75,26 +89,12 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
@ -102,7 +102,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,6 @@
2000 LET SUM=0
2010 FOR F=1 TO N-1
2020 IF N/F=INT (N/F) THEN LET SUM=SUM+F
2030 NEXT F
2040 LET PERFECT=SUM=N
2050 RETURN

View file

@ -1,24 +0,0 @@
class
PERFECT_NUMBERS
feature
perfect(n:INTEGER):BOOLEAN
require
n_positive: n>0
local
sum, i: INTEGER
do
from
i:=1
until
i=n
loop
if n\\i=0 then
sum:=sum+i
end
i:= i+1
end
if sum= n then
RESULT:= TRUE
end
end
end

View file

@ -1,20 +0,0 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create perfect
io.put_string ("%N 6 is perfect...%T")
io.put_boolean (perfect.perfect (6))
io.put_string ("%N77 is perfect...%T")
io.put_boolean (perfect.perfect (77))
io.put_string ("%N496 is perfect...%T")
io.put_boolean (perfect.perfect (496))
end
perfect: PERFECT_NUMBERS
end

View file

@ -1,20 +1,19 @@
#import system.
#import system'routines.
#import system'math.
#import extensions.
import system'routines.
import system'math.
import extensions.
#class(extension)extension
extension extension
{
#method is &perfect
= 1 repeat &till:self &each: n [ (self mod:n == 0) iif:n:0 ] summarize:(Integer new) == self.
isPerfect
= 1 till:self repeat(:n)( (self mod:n == 0) iif(n,0) ); summarize(Integer new) == self.
}
#symbol program =
program =
[
1 till:10000 &doEach: n
1 till:10000 do(:n)
[
(n is &perfect)
? [ console writeLine:n:" is perfect". ].
if(n isPerfect)
[ console printLine(n," is perfect") ]
].
console readChar.

View file

@ -1,15 +1,19 @@
perfect = map (\x -> (2^x - 1) * (2^(x - 1))) $
filter (\x -> isPrime x && isPrime (2^x - 1)) maybe_prime where
maybe_prime = scanl1 (+) (2:1:cycle [2,2,4,2,4,2,4,6])
isPrime n = all ((/=0).(n`mod`)) $
takeWhile (\x -> x*x <= n) maybe_prime
perfect =
(\x -> (2 ^ x - 1) * (2 ^ (x - 1))) <$>
filter (\x -> isPrime x && isPrime (2 ^ x - 1)) maybe_prime
where
maybe_prime = scanl1 (+) (2 : 1 : cycle [2, 2, 4, 2, 4, 2, 4, 6])
isPrime n = all ((/= 0) . (n `mod`)) $ takeWhile (\x -> x * x <= n) maybe_prime
isPerfect n = f n perfect where
f n (p:ps) = case compare n p of
EQ -> True
LT -> False
GT -> f n ps
isPerfect n = f n perfect
where
f n (p:ps) =
case compare n p of
EQ -> True
LT -> False
GT -> f n ps
main :: IO ()
main = do
mapM_ print $ take 10 perfect
mapM_ print $ map (\x -> (x, isPerfect x)) [6,27,28,29,496,8128,8129]
mapM_ print $ take 10 perfect
mapM_ (print . (\x -> (x, isPerfect x))) [6, 27, 28, 29, 496, 8128, 8129]

View file

@ -0,0 +1,24 @@
// version 1.0.6
fun isPerfect(n: Int): Boolean = when {
n < 2 -> false
n % 2 == 1 -> false // there are no known odd perfect numbers
else -> {
var tot = 1
var q: Int
for (i in 2 .. Math.sqrt(n.toDouble()).toInt()) {
if (n % i == 0) {
tot += i
q = n / i
if (q > i) tot += q
}
}
n == tot
}
}
fun main(args: Array<String>) {
// expect a run time of about 6 minutes on a typical laptop
println("The first five perfect numbers are:")
for (i in 2 .. 33550336) if (isPerfect(i)) print("$i ")
}

View file

@ -1,14 +0,0 @@
import math
proc isperfect(n: int): bool =
let max: int = (sqrt(n.toFloat)).toInt
var sum: int = 1
for i in 2..max:
if n mod i == 0:
let q: int = n div i
sum += (i + q)
return (n == sum)
for i in 2..10_000:
if isperfect(i):
echo(i)

View file

@ -0,0 +1,17 @@
-- first perfect number over 10000 is 33550336...let's not be crazy
loop i = 1 to 10000
if perfectNumber(i) then say i "is a perfect number"
end
::routine perfectNumber
use strict arg n
sum = 0
-- the largest possible factor is n % 2, so no point in
-- going higher than that
loop i = 1 to n % 2
if n // i == 0 then sum += i
end
return sum = n

View file

@ -1,13 +1,10 @@
use ntheory qw(is_mersenne_prime valuation hammingweight is_power sqrtint);
use ntheory qw(is_mersenne_prime valuation);
sub is_even_perfect {
my ($n) = @_;
$n % 2 == 0 || return;
my $square = 8 * $n + 1;
is_power($square, 2) || return;
my $k = (sqrtint($square) + 1) / 2;
hammingweight($k) == 1 && is_mersenne_prime(valuation($k, 2));
my $v = valuation($n, 2) || return;
my $m = ($n >> $v);
($m & ($m + 1)) && return;
($m >> $v) == 1 || return;
is_mersenne_prime($v + 1);
}

View file

@ -0,0 +1,21 @@
fn main ( ) {
fn factor_sum(n: i32) -> i32 {
let mut v = Vec::new(); //create new empty array
for x in 1..n-1 { //test vaules 1 to n-1
if n%x == 0 { //if current x is a factor of n
v.push(x); //add x to the array
}
}
let mut sum = v.iter().sum(); //iterate over array and sum it up
return sum;
}
fn perfect_nums(n: i32) {
for x in 2..n { //test numbers from 1-n
if factor_sum(x) == x {//call factor_sum on each value of x, if return value is = x
println!("{} is a perfect number.", x); //print value of x
}
}
}
perfect_nums(10000);
}

View file

@ -0,0 +1,2 @@
fcn isPerfectNumber1(n)
{ n == [1..n-1].filter('wrap(i){ n % i == 0 }).sum(); }