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,44 @@
' FB 1.05.0 Win64
Function isHappy(n As Integer) As Boolean
If n < 0 Then Return False
' Declare a dynamic array to store previous sums.
' If a previous sum is duplicated before a sum of 1 is reached
' then the number can't be "happy" as the cycle will just repeat
Dim prevSums() As Integer
Dim As Integer digit, ub, sum = 0
Do
While n > 0
digit = n Mod 10
sum += digit * digit
n \= 10
Wend
If sum = 1 Then Return True
ub = UBound(prevSums)
If ub > -1 Then
For i As Integer = 0 To ub
If sum = prevSums(i) Then Return False
Next
End If
ub += 1
Redim Preserve prevSums(0 To ub)
prevSums(ub) = sum
n = sum
sum = 0
Loop
End Function
Dim As Integer n = 1, count = 0
Print "The first 8 happy numbers are : "
Print
While count < 8
If isHappy(n) Then
count += 1
Print count;" =>"; n
End If
n += 1
Wend
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,33 @@
PROCEDURE Main()
LOCAL i := 8, nH := 0
? hb_StrFormat( "The first %d happy numbers are:", i )
?
WHILE i > 0
IF IsHappy( ++nH )
?? hb_NtoS( nH ) + " "
--i
ENDIF
END
RETURN
STATIC FUNCTION IsHappy( nNumber )
STATIC aUnhappy := {}
LOCAL nDigit, nSum := 0, cNumber := hb_NtoS( nNumber )
FOR EACH nDigit IN cNumber
nSum += Val( nDigit ) ^ 2
NEXT
IF nSum == 1
aUnhappy := {}
RETURN .T.
ELSEIF AScan( aUnhappy, nSum ) > 0
RETURN .F.
ENDIF
AAdd( aUnhappy, nSum )
RETURN IsHappy( nSum )

View file

@ -0,0 +1,15 @@
#!/usr/bin/lasso9
define isHappy(n::integer) => {
local(past = set)
while(#n != 1) => {
#n = with i in string(#n)->values sum math_pow(integer(#i), 2)
#past->contains(#n) ? return false | #past->insert(#n)
}
return true
}
with x in generateSeries(1, 500)
where isHappy(#x)
take 8
select #x

View file

@ -0,0 +1 @@
1, 7, 10, 13, 19, 23, 28, 31

View file

@ -0,0 +1,20 @@
import intsets
proc happy(n: int): bool =
var
n = n
past = initIntSet()
while n != 1:
let s = $n
n = 0
for c in s:
let i = ord(c) - ord('0')
n += i * i
if n in past:
return false
past.incl(n)
return true
for x in 0..31:
if happy(x):
echo x

View file

@ -0,0 +1,16 @@
: isHappy(n)
| cycle |
ListBuffer new ->cycle
while(n 1 <>) [
cycle include(n) ifTrue: [ false return ]
cycle add(n)
0 n asString apply(#[ asDigit sq + ]) ->n
]
true ;
: happyNum(N)
| numbers |
ListBuffer new ->numbers
1 while(numbers size N <>) [ dup isHappy ifTrue: [ dup numbers add ] 1+ ]
numbers println ;

View file

@ -0,0 +1,27 @@
function is_happy(integer n)
sequence seen = {}
integer k
while n>1 do
seen &= n
k = 0
while n>0 do
k += power(remainder(n,10),2)
n = floor(n/10)
end while
n = k
if find(n,seen) then
return 0
end if
end while
return 1
end function
integer n = 1
sequence s = {}
while length(s)<8 do
if is_happy(n) then
s &= n
end if
n += 1
end while
?s

View file

@ -0,0 +1,20 @@
sqr = (n): n * n.
isHappy = (n) :
loop :
if (n == 1): return true.
if (n == 4): return false.
sum = 0
n = n string
n length times (i): sum = sum + sqr(n(i) number integer).
n = sum
.
.
firstEight = ()
i = 0
while (firstEight length < 8) :
i++
if (isHappy(i)): firstEight append(i).
.
firstEight string print

View file

@ -0,0 +1,24 @@
n = 1
found = 0
While found < 8
If IsHappy(n)
found += 1
see string(found) + " : " + string(n) + nl
ok
n += 1
End
Func IsHappy n
cache = []
While n != 1
Add(cache,n)
t = 0
strn = string(n)
for e in strn
t += pow(number(e),2)
next
n = t
If find(cache,n) Return False ok
End
Return True

View file

@ -0,0 +1,27 @@
import <Utilities/Math.sl>;
import <Utilities/Conversion.sl>;
main(argv(2)) := findHappys(stringToInt(head(argv)));
findHappys(count) := findHappysHelper(count, 1, []);
findHappysHelper(count, n, happys(1)) :=
happys when size(happys) = count
else
findHappysHelper(count, n + 1, happys ++ [n]) when isHappy(n)
else
findHappysHelper(count, n + 1, happys);
isHappy(n) := isHappyHelper(n, []);
isHappyHelper(n, cache(1)) :=
let
digits[i] := (n / integerPower(10, i - 1)) mod 10
foreach i within 1 ... ceiling(log(10, n + 1));
newN := sum(integerPower(digits, 2));
in
false when some(n = cache)
else
true when n = 1
else
isHappyHelper(newN, cache ++ [n]);

View file

@ -0,0 +1,15 @@
func happy(n) is cached {
static seen = Hash()
return true if n.is_one
return false if seen.has_key(n)
seen{n} = 1
happy(n.digits »**» 2 -> sum)
}
var count = 0;
Inf.times { |i|
happy(i) ? say i : next
++count == 8 && break
}

View file

@ -0,0 +1,25 @@
func isHappyNumber(var n:Int) -> Bool {
var cycle = [Int]()
while n != 1 && !cycle.contains(n) {
cycle.append(n)
var m = 0
while n > 0 {
let d = n % 10
m += d * d
n = (n - d) / 10
}
n = m
}
return n == 1
}
var found = 0
var count = 0
while found != 8 {
if isHappyNumber(count) {
print(count)
found++
}
count++
}

View file

@ -0,0 +1,15 @@
def is_happy_number:
def next: tostring | explode | map( (. - 48) | .*.) | add;
def last(g): reduce g as $i (null; $i);
# state: either 1 or [i, o]
# where o is an an object with the previously encountered numbers as keys
def loop:
recurse( if . == 1 then empty # all done
elif .[0] == 1 then 1 # emit 1
else (.[0]| next) as $n
| if $n == 1 then 1
elif .[1]|has($n|tostring) then empty
else [$n, (.[1] + {($n|tostring):true}) ]
end
end );
1 == last( [.,{}] | loop );

View file

@ -0,0 +1,12 @@
# Set n to -1 to continue indefinitely:
def happy(n):
def subtask: # state: [i, found]
if .[1] == n then empty
else .[0] as $n
| if ($n | is_happy_number) then $n, ([ $n+1, .[1]+1 ] | subtask)
else (.[0] += 1) | subtask
end
end;
[0,0] | subtask;
happy($n|tonumber)

View file

@ -0,0 +1,9 @@
$ jq --arg n 8 -n -f happy.jq
1
7
10
13
19
23
28
31