Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,65 @@
' version 04-12-2016
' compile with: fbc -s console
' define true and false for older versions
#Ifndef TRUE
#Define FALSE 0
#Define TRUE Not FALSE
#EndIf
#Define max 1000000 ' maximum for number to be tested
Function kaprekar(n As ULong) As ULong
If n = 1 Then Return TRUE
Dim As ULong x, p1, p2
Dim As ULongInt sq = CLngInt(n) * n
Dim As String sq_str = Str(sq)
Dim As ULong l = Len(sq_str)
' decrease the lenght l for every "0"
' at the end of the string
For x = l -1 To 1 Step -1
If sq_str[x] = Asc("0") Then
l = l -1
Else
Exit For
End If
Next
For x = 1 To l -1
p2 = Val(Mid(sq_str, x +1))
If p2 > n Then
Continue For
End If
p1 = Val(Left(sq_str, x))
If p1 > n Then Return FALSE ' p1 > n leave
If (p1 + p2) = n Then Return TRUE
Next
End Function
' ------=< MAIN >=------
Dim As ULong n, count
Print "Kaprekar numbers below 10000"
For n = 1 To max -1
If kaprekar(n) = TRUE Then
count = count + 1
If n < 10000 Then
Print count, n
End If
End If
Next
Print
Print count;" numbers below "; Str(max);" are Kaprekar numbers"
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,12 @@
import strutils, sequtils
proc k(n): bool =
let n2 = $(n.in64 * n)
for i in 0 .. <n2.len:
let a = if i > 0: parseBiggestInt n2[0 .. <i] else: 0
let b = parseBiggestInt n2[i .. n2.high]
if b > 0 and a + b == n:
return true
echo toSeq(1..10_000).filter(k)
echo len toSeq(1..1_000_000).filter(k)

View file

@ -0,0 +1,18 @@
nr = 0
for i = 1 to 200
if kaprekar(i)
nr += 1
if i < 201 see "" + nr + " : " + i + nl ok ok
next
see "total kaprekar numbers under 200 = " + nr + nl
func kaprekar n
s = pow(n,2)
x = floor(log(s)) + 1
t = pow(10,x)
while true
t /= 10
if t<=n exit ok
if s-n = floor(s/t)*(t-1) n = true ok
end
return (n = 1)

View file

@ -0,0 +1,19 @@
require('ntheory')
var kap = Hash()
15.times { |n|
var np = (10**n - 1)
%S<ntheory>.fordivisors({ |d|
var dp = np//d
if (gcd(d, dp) == 1) {
kap{ dp == 1 ? d : d.invmod(dp)*d } := 0 ++
}
}, np)
}
var nums = kap.keys.map{.to_n}.sort
for n in (6 .. 14) {
var np = (10**n - 1)
printf("Kaprekar numbers <= 10^%2d: %5d\n", n, nums.count_by { .<= np })
}

View file

@ -0,0 +1,19 @@
@let {
isKap &n [
@var s +'' *n n
@for i til +1/#s 2 [
@vars {
fn @+!!s.slice 0 i
sn @+!s.slice i
}
@if =0 sn @break
@if =n +fn sn @return [fn sn]
]
false
]
~[
!console.log "Kaprekar numbers below 10000: {!-isKap @to 1TK}"
!console.log "Number of Kaprekar numbers below 1000000: {#!-isKap @to 1M}"
]
}

View file

@ -0,0 +1,32 @@
# Is the input integer a Kaprekar integer?
def is_kaprekar:
# The helper function acts like a loop:
# input is [n, number, str]
# where n is the position to be considered next,
# number is the integer under consideration,
# and str is the string representing number*number
def _try:
.[0] as $n | .[1] as $number | .[2] as $str
| if $n >= ($str|length) then null
else ($str[0:$n] | tonumber) as $left
| ($str[$n:] | tonumber) as $right
| if $left > $number then null
elif $right == 0 then null
elif ($left + $right) == $number then $n
else [($n + 1), $number, $str] | _try
end
end;
. as $in
| if . == 1 then true
elif . < 1 then false
else null != ([1, $in, ($in*$in|tostring)] | _try)
end ;
# Useful for counting how many times the condition is satisfied:
def count(generator; condition):
reduce generator as $i (0; if ($i|condition ) then .+1 else . end);
def task:
[ range(1;10000) | select( is_kaprekar ) ],
count( range(1;1000000); is_kaprekar )
;

View file

@ -0,0 +1,3 @@
$ jq -n -c -f is_kaprekar.jq
[1,9,45,55,99,297,703,999,2223,2728,4879,4950,5050,5292,7272,7777,9999]
54