Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,79 @@
|
|||
program DuffinianNumbers;
|
||||
|
||||
CONST
|
||||
MaxSigma = 10000;
|
||||
|
||||
VAR
|
||||
seen, cur: CARDINAL;
|
||||
sigma: ARRAY [1..MaxSigma] OF CARDINAL;
|
||||
|
||||
PROCEDURE CalculateSigmaTable;
|
||||
VAR i, j: CARDINAL;
|
||||
BEGIN
|
||||
FOR i := 1 TO MaxSigma DO
|
||||
BEGIN
|
||||
sigma[i] := 0
|
||||
END;
|
||||
FOR i := 1 TO MaxSigma DO
|
||||
BEGIN
|
||||
j := i;//2*i -> sigma(n)-n
|
||||
WHILE j <= MaxSigma DO
|
||||
Begin
|
||||
INC(sigma[j], i);
|
||||
INC(j, i);
|
||||
END
|
||||
END
|
||||
END;// CalculateSigmaTable;
|
||||
|
||||
FUNCTION GCD(a, b: CARDINAL): CARDINAL;
|
||||
VAR c: CARDINAL;
|
||||
BEGIN
|
||||
WHILE b <>0 DO
|
||||
Begin
|
||||
c := a MOD b;
|
||||
a := b;
|
||||
b := c
|
||||
END;
|
||||
EXIT(A)
|
||||
END;// GCD;
|
||||
|
||||
function IsDuffinian(n: CARDINAL): BOOLEAN;
|
||||
BEGIN
|
||||
EXIT( (sigma[n] > n+1) AND (GCD(n, sigma[n]) = 1))
|
||||
END;// IsDuffinian;
|
||||
|
||||
FUNCTION IsDuffinianTriple(n: CARDINAL): BOOLEAN;
|
||||
BEGIN
|
||||
EXIT(IsDuffinian(n) AND IsDuffinian(n+1) AND IsDuffinian(n+2));
|
||||
END;// IsDuffinianTriple;
|
||||
|
||||
BEGIN
|
||||
CalculateSigmaTable;
|
||||
Writeln('First 50 Duffinian numbers:');
|
||||
WriteLn;
|
||||
cur := 0;
|
||||
FOR seen := 1 TO 50 DO
|
||||
Begin
|
||||
REPEAT
|
||||
INC(cur)
|
||||
UNTIL IsDuffinian(cur);
|
||||
Write(cur:4);
|
||||
IF seen MOD 10 = 0 THEN
|
||||
WriteLn;
|
||||
END;
|
||||
|
||||
WriteLn;
|
||||
WriteLn('First 15 Duffinian triples:');
|
||||
WriteLn;
|
||||
cur := 0;
|
||||
FOR seen := 1 TO 15 DO
|
||||
BEGIN
|
||||
REPEAT
|
||||
INC(cur)
|
||||
UNTIL IsDuffinianTriple(cur);
|
||||
Write(cur:6);
|
||||
Write(cur+1:6);
|
||||
Write(cur+2:6);
|
||||
WriteLn;
|
||||
END
|
||||
END.// DuffinianNumbers.
|
||||
50
Task/Duffinian-numbers/Haskell/duffinian-numbers.hs
Normal file
50
Task/Duffinian-numbers/Haskell/duffinian-numbers.hs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import Data.List ( tail )
|
||||
import qualified Data.Set as S
|
||||
import Data.List.Split ( divvy )
|
||||
|
||||
divisors :: Int -> [Int]
|
||||
divisors n = [d | d <- [1..n] , mod n d == 0]
|
||||
|
||||
primeFactors :: Int -> [Int]
|
||||
primeFactors n = snd $ until ( (== 1 ) . fst ) step (n , [] )
|
||||
where
|
||||
step :: (Int , [Int]) -> (Int , [Int])
|
||||
step ( aNumber , factors ) = ( div aNumber smallest , factors ++
|
||||
[smallest] )
|
||||
where
|
||||
smallest :: Int
|
||||
smallest = head $ tail $ divisors aNumber
|
||||
|
||||
isRelativelyPrime :: Int -> Int -> Bool
|
||||
isRelativelyPrime a b = S.null $ S.intersection ( S.fromList $
|
||||
primeFactors a ) ( S.fromList $ primeFactors b )
|
||||
|
||||
isDuffinian :: Int -> Bool
|
||||
isDuffinian n = and [(length $ primeFactors n ) > 1 , isRelativelyPrime
|
||||
( sum $ divisors n ) n]
|
||||
|
||||
solution :: [Int]
|
||||
solution = take 50 $ filter isDuffinian [1..]
|
||||
|
||||
findTriplets :: [[Int]]
|
||||
findTriplets = take 15 $ filter condition $ divvy 3 1 $ filter
|
||||
isDuffinian [1..]
|
||||
where
|
||||
condition :: [Int] -> Bool
|
||||
condition [a , b , c] = b == a + 1 && c == b + 1
|
||||
|
||||
outputTriplet :: [Int] -> String
|
||||
outputTriplet [a , b , c] = "(" ++ replicate ( 6 - length sa ) ' ' ++
|
||||
sa ++ replicate (6 - length sb) ' ' ++ sb ++ replicate (6 - length sc )
|
||||
' ' ++ sc ++ " )"
|
||||
where
|
||||
sa = show a
|
||||
sb = show b
|
||||
sc = show c
|
||||
|
||||
main :: IO ( )
|
||||
main = do
|
||||
putStrLn "The first 50 Duffinian numbers are :"
|
||||
print solution
|
||||
putStrLn "The first 15 Duffinian triplets are:"
|
||||
mapM_ (\t -> putStrLn $ outputTriplet t ) findTriplets
|
||||
28
Task/Duffinian-numbers/Icon/duffinian-numbers.icon
Normal file
28
Task/Duffinian-numbers/Icon/duffinian-numbers.icon
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
procedure main(A)
|
||||
write("The first 50 Duffian numbers:")
|
||||
every writes(" ",isduffinian(seq())\50)
|
||||
write("\n")
|
||||
limit := 15
|
||||
write("The first ",limit," Duffian triples:")
|
||||
every (isduffinian(n := seq()),isduffinian(n+1),isduffinian(n+2))\limit do
|
||||
write("\t(",n,",",n+1,",",n+2,")")
|
||||
end
|
||||
|
||||
procedure isduffinian(n)
|
||||
x := \cfact(n)**\afact(sumfactors(n))
|
||||
return (*\x = 1, !x = 1, n)
|
||||
end
|
||||
|
||||
procedure cfact(n) # all factors of n if n is a composite number
|
||||
return (*(f := afact(n)) > 2, f)
|
||||
end
|
||||
|
||||
procedure afact(n) # all factors of n
|
||||
every (f := set(), i := 1 to n, n%i = 0,insert(f,i))
|
||||
return f
|
||||
end
|
||||
|
||||
procedure sumfactors(n)
|
||||
every (s := 0, i := 1 to n, n%i = 0) do s +:= i
|
||||
return s
|
||||
end
|
||||
49
Task/Duffinian-numbers/JavaScript/duffinian-numbers.js
Normal file
49
Task/Duffinian-numbers/JavaScript/duffinian-numbers.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
'use strict';
|
||||
|
||||
// ---------- helpers ----------
|
||||
const gcd = (a, b) => {
|
||||
while (b !== 0) [a, b] = [b, a % b];
|
||||
return a;
|
||||
};
|
||||
|
||||
// ---------- core logic ----------
|
||||
function createDuffians(limit) {
|
||||
// divisor-sum table: divSum[i] = σ(i)
|
||||
const divSum = new Array(limit).fill(1);
|
||||
for (let i = 2; i < limit; ++i) {
|
||||
for (let j = i; j < limit; j += i) divSum[j] += i;
|
||||
}
|
||||
|
||||
// mark non-Duffinians with 0
|
||||
divSum[1] = 0; // 1 is not Duffinian
|
||||
for (let n = 2; n < limit; ++n) {
|
||||
const s = divSum[n];
|
||||
if (s === n + 1 || gcd(n, s) !== 1) divSum[n] = 0;
|
||||
}
|
||||
return divSum;
|
||||
}
|
||||
|
||||
// ---------- main ----------
|
||||
const duffians = createDuffians(11_000);
|
||||
|
||||
console.log('The first 50 Duffinian numbers:');
|
||||
let count = 0, n = 1;
|
||||
while (count < 50) {
|
||||
if (duffians[n] > 0) {
|
||||
process.stdout.write(String(n).padStart(4) + (++count % 25 ? '' : '\n'));
|
||||
}
|
||||
++n;
|
||||
}
|
||||
console.log();
|
||||
|
||||
console.log('The first 16 Duffinian triplets:');
|
||||
count = 0;
|
||||
n = 3;
|
||||
while (count < 16) {
|
||||
if (duffians[n - 2] && duffians[n - 1] && duffians[n]) {
|
||||
const triplet = `(${n - 2}, ${n - 1}, ${n})`;
|
||||
process.stdout.write(triplet.padStart(22) + (++count % 4 ? '' : '\n'));
|
||||
}
|
||||
++n;
|
||||
}
|
||||
console.log();
|
||||
97
Task/Duffinian-numbers/Ring/duffinian-numbers.ring
Normal file
97
Task/Duffinian-numbers/Ring/duffinian-numbers.ring
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
Load "stdlibcore.ring"
|
||||
|
||||
nr = 0
|
||||
num = 0
|
||||
sig1 = 0
|
||||
cmp = 0
|
||||
|
||||
see "FIRST 50 DUFFINIAN NUMBERS:" + nl
|
||||
|
||||
while nr < 50
|
||||
num = num + 1
|
||||
sigma = 0
|
||||
cmp = comp(num)
|
||||
if cmp = 1
|
||||
sgm = sigm1(num)
|
||||
relat1prim()
|
||||
ok
|
||||
end
|
||||
|
||||
nr = 0
|
||||
fok = 0
|
||||
numc = 1
|
||||
sig1 = 0
|
||||
sig2 = 0
|
||||
sig3 = 0
|
||||
|
||||
see nl + "FIRST 15 DUFFINIAN TRIPLETS:" + nl
|
||||
|
||||
while nr < 15
|
||||
|
||||
num1 = numc
|
||||
num2 = num1 + 1
|
||||
num3 = num2 + 1
|
||||
cmp1 = comp(num1)
|
||||
cmp2 = comp(num2)
|
||||
cmp3 = comp(num3)
|
||||
if (cmp1 = 1) and (cmp2 = 1) and (cmp3 = 1)
|
||||
sigm1(num1)
|
||||
sigm2(num2)
|
||||
sigm3(num3)
|
||||
ok
|
||||
numc = numc + 1
|
||||
end
|
||||
|
||||
func comp(nm)
|
||||
fok = 0
|
||||
flag = 0
|
||||
for n = 1 to nm
|
||||
if nm%n = 0
|
||||
flag = flag + 1
|
||||
ok
|
||||
if flag > 2
|
||||
fok = 1
|
||||
exit
|
||||
ok
|
||||
next
|
||||
return fok
|
||||
|
||||
func sigm1(num1)
|
||||
sig1 = 0
|
||||
for n = 1 to num1
|
||||
if num1%n = 0
|
||||
sig1 = sig1 + n
|
||||
ok
|
||||
next
|
||||
|
||||
func sigm2(num2)
|
||||
sig2 = 0
|
||||
for n = 1 to num2
|
||||
if num2%n = 0
|
||||
sig2 = sig2 + n
|
||||
ok
|
||||
next
|
||||
|
||||
func sigm3(num3)
|
||||
sig3 = 0
|
||||
for n = 1 to num3
|
||||
if num3%n = 0
|
||||
sig3 = sig3 + n
|
||||
ok
|
||||
next
|
||||
|
||||
relat2prim()
|
||||
|
||||
func relat1prim()
|
||||
if (gcd(num,sig1) = 1)
|
||||
nr = nr + 1
|
||||
see "" + num + " "
|
||||
ok
|
||||
return nr
|
||||
|
||||
func relat2prim()
|
||||
if ((gcd(num1,sig1) = 1) and (gcd(num2,sig2) = 1) and (gcd(num3,sig3) = 1))
|
||||
nr = nr + 1
|
||||
see "" + num1 + "-" + num3 + " "
|
||||
ok
|
||||
return nr
|
||||
Loading…
Add table
Add a link
Reference in a new issue