June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,11 @@
USING: kernel math.parser prettyprint sequences ;
IN: rosetta-code.self-describing-numbers
: digits ( n -- seq ) number>string string>digits ;
: digit-count ( seq n -- m ) [ = ] curry count ;
: self-describing-number? ( n -- ? )
digits dup [ digit-count = ] with map-index [ t = ] all? ;
100,000,000 <iota> [ self-describing-number? ] filter .

View file

@ -4,13 +4,14 @@ count :: Int -> [Int] -> Int
count x = length . filter (x ==)
isSelfDescribing :: Integer -> Bool
isSelfDescribing n =
nu == f where
nu = map digitToInt (show n)
f = map (\a -> count a nu) [0 .. ((length nu)-1)]
isSelfDescribing n = nu == f
where
nu = digitToInt <$> show n
f = (`count` nu) <$> [0 .. length nu - 1]
main :: IO ()
main = do
let tests = [1210, 2020, 21200, 3211000,
42101000, 521001000, 6210001000]
print $ map isSelfDescribing tests
print $ filter isSelfDescribing [0 .. 4000000]
print $
isSelfDescribing <$>
[1210, 2020, 21200, 3211000, 42101000, 521001000, 6210001000]
print $ filter isSelfDescribing [0 .. 4000000]

View file

@ -7,15 +7,16 @@ count x = length . filter (x ==)
-- all the combinations of n digits of base n
-- a base-n number are represented as a list of ints, one per digit
allBaseNNumsOfLength :: Int -> [[Int]]
allBaseNNumsOfLength n = replicateM n [0..n-1]
allBaseNNumsOfLength = replicateM <*> (enumFromTo 0 . subtract 1)
isSelfDescribing :: [Int] -> Bool
isSelfDescribing num =
all (\(i,x) -> x == count i num) $ zip [0..] num
isSelfDescribing num = all (\(i, x) -> x == count i num) $ zip [0 ..] num
-- translate it back into an integer in base-10
decimalize :: [Int] -> Int
decimalize = read . map intToDigit
main = forM_ [1..7] $
print . map decimalize . filter isSelfDescribing . allBaseNNumsOfLength
main :: IO ()
main =
(print . concat) $
map decimalize . filter isSelfDescribing . allBaseNNumsOfLength <$> [1 .. 8]

View file

@ -1,9 +1,14 @@
function selfie(x)
y = reverse(digits(x))
len = length(y)
sum(y) != len && return false
for i = 1:len
y[i] != sum(y .== i-1) && return false
function selfie(x::Integer)
ds = reverse(digits(x))
if sum(ds) != length(ds) return false end
for (i, d) in enumerate(ds)
if d != sum(ds .== i - 1) return false end
end
return true
end
@show selfie(2020)
@show selfie(2021)
selfies(x) = for i in 1:x selfie(i) && println(i) end
@time selfies(4000000)

View file

@ -0,0 +1,51 @@
MODULE SelfDescribingNumber;
FROM WholeStr IMPORT
CardToStr;
FROM STextIO IMPORT
WriteString, WriteLn;
FROM SWholeIO IMPORT
WriteCard;
PROCEDURE Check(Number: CARDINAL): BOOLEAN;
VAR
I, D: CARDINAL;
A: ARRAY [0 .. 9] OF CHAR;
Count, W: ARRAY [0 .. 9] OF CARDINAL;
Result: BOOLEAN;
BEGIN
CardToStr(Number, A);
FOR I := 0 TO 9 DO
Count[I] := 0;
W[I] := 0;
END;
FOR I := 0 TO LENGTH(A) - 1 DO
D := ORD(A[I]) - ORD("0");
INC(Count[D]);
W[I] := D;
END;
Result := TRUE;
I := 0;
WHILE Result AND (I <= 9) DO
Result := (Count[I] = W[I]);
INC(I);
END;
RETURN Result;
END Check;
VAR
X: CARDINAL;
BEGIN
WriteString("Autodescriptive numbers from 1 to 100000000:");
WriteLn;
FOR X := 1 TO 100000000 DO
IF Check(X) THEN
WriteString(" ");
WriteCard(X, 1);
WriteLn;
END;
END;
WriteString("Job done.");
WriteLn;
END SelfDescribingNumber.

View file

@ -0,0 +1,30 @@
Red []
;;-------------------------------------
count-dig: func ["count occurence of digit in number"
;;-------------------------------------
s [string!] "number as string"
sdig [char!] "search number as char"
][
cnt: #"0" ;; counter as char for performance optimization
while [s: find/tail s sdig][cnt: cnt + 1]
return cnt
]
;;-------------------------------------
isSDN?: func ["test if number is self describing number"
s [string!] "number to test as string "
][
;;-------------------------------------
ind: #"0" ;; use digit as char for performance optimization
foreach ele s [
if ele <> count-dig s ind [return false]
ind: ind + 1
]
return true
]
repeat i 4000000 [ if isSDN? to-string i [print i] ]

View file

@ -0,0 +1,27 @@
# Project : Self-describing numbers
# Date : 2017/11/18
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
for num = 1 to 45000000
res = 0
for n=1 to len(string(num))
temp = string(num)
pos = number(temp[n])
cnt = count(temp,string(n-1))
if cnt = pos
res = res + 1
ok
next
if res = len(string(num))
see num + nl
ok
next
func count(cString,dString)
sum = 0
while substr(cString,dString) > 0
sum = sum + 1
cString = substr(cString,substr(cString,dString)+len(string(sum)))
end
return sum

View file

@ -0,0 +1,22 @@
fn is_self_desc(xx: u64) -> bool
{
let s: String = xx.to_string();
let mut count_vec = vec![0; 10];
for c in s.chars() {
count_vec[c.to_digit(10).unwrap() as usize] += 1;
}
for (i, c) in s.chars().enumerate() {
if count_vec[i] != c.to_digit(10).unwrap() as usize {
return false;
}
}
return true;
}
fn main() {
for i in 1..100000000 {
if is_self_desc(i) {
println!("{}", i)
}
}
}

View file

@ -0,0 +1,14 @@
object SelfDescribingNumbers extends App {
def isSelfDescribing(a: Int): Boolean = {
val s = Integer.toString(a)
(0 until s.length).forall(i => s.count(_.toString.toInt == i) == s(i).toString.toInt)
}
println("Curious numbers n = x0 x1 x2...x9 such that xi is the number of digits equal to i in n.")
for (i <- 0 to 42101000 by 10
if isSelfDescribing(i)) println(i)
println("Successfully completed without errors.")
}