Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
shared void run() {
|
||||
|
||||
function digitsSquaredSum(variable Integer n) {
|
||||
variable value total = 0;
|
||||
while(n > 0) {
|
||||
total += (n % 10) ^ 2;
|
||||
n /= 10;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
function lastSum(variable Integer n) {
|
||||
while(true) {
|
||||
n = digitsSquaredSum(n);
|
||||
if(n == 89 || n == 1) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable value eightyNines = 0;
|
||||
for(i in 1..100M - 1) {
|
||||
if(lastSum(i) == 89) {
|
||||
eightyNines++;
|
||||
}
|
||||
}
|
||||
print(eightyNines);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
PROGRAM ITERATION
|
||||
|
||||
BEGIN
|
||||
PRINT(CHR$(12);) ! CLS
|
||||
INPUT(N)
|
||||
LOOP
|
||||
N$=MID$(STR$(N),2)
|
||||
S=0
|
||||
FOR I=1 TO LEN(N$) DO
|
||||
A=VAL(MID$(N$,I,1))
|
||||
S=S+A*A
|
||||
END FOR
|
||||
IF S=89 OR S=1 THEN PRINT(S;) EXIT END IF
|
||||
PRINT(S;)
|
||||
N=S
|
||||
END LOOP
|
||||
PRINT
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' similar to C Language (first approach)
|
||||
' timing for i3 @ 2.13 GHz
|
||||
|
||||
Function endsWith89(n As Integer) As Boolean
|
||||
Dim As Integer digit, sum = 0
|
||||
Do
|
||||
While n > 0
|
||||
digit = n Mod 10
|
||||
sum += digit * digit
|
||||
n \= 10
|
||||
Wend
|
||||
If sum = 89 Then Return True
|
||||
If sum = 1 Then Return False
|
||||
n = sum
|
||||
sum = 0
|
||||
Loop
|
||||
End Function
|
||||
|
||||
Dim As Double start = timer
|
||||
Dim sums(0 To 8 * 81) As UInteger
|
||||
sums(0) = 1
|
||||
sums(1) = 0
|
||||
Dim s As Integer
|
||||
For n As Integer = 1 To 8
|
||||
For i As Integer = n * 81 To 1 Step -1
|
||||
For j As Integer = 1 To 9
|
||||
s = j * j
|
||||
If s > i Then Exit For
|
||||
sums(i) += sums(i - s)
|
||||
Next j
|
||||
Next i
|
||||
|
||||
If n = 8 Then
|
||||
Dim As UInteger count89 = 0
|
||||
For i As Integer = 1 To n * 81
|
||||
If Not endsWith89(i) Then Continue For
|
||||
count89 += sums(i)
|
||||
Next i
|
||||
Print "There are";count89; " numbers from 1 to 100 million ending with 89"
|
||||
End If
|
||||
Next
|
||||
Print "Elapsed milliseconds ="; Int((timer - start) * 1000 + 0.5)
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
: sq_digits(n)
|
||||
while (n 1 <> n 89 <> and ) [
|
||||
0 while(n) [ n 10 /mod ->n dup * + ]
|
||||
->n
|
||||
] n ;
|
||||
|
||||
: iterDigits | i | 0 100000000 loop: i [ i sq_digits 89 &= + ] . ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
nr = 1000
|
||||
num = 0
|
||||
for n = 1 to nr
|
||||
sum = 0
|
||||
for m = 1 to len(string(n))
|
||||
sum += pow(number(substr(string(n),m,1)),2)
|
||||
if sum = 89 num += 1 see "" + n + " " + sum + nl ok
|
||||
next
|
||||
next
|
||||
see "Total under 1000 is : " + num + nl
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
def factorial: reduce range(2;.+1) as $i (1; . * $i);
|
||||
|
||||
# Pick n items (with replacement) from the input array,
|
||||
# but only consider distinct combinations:
|
||||
def pick(n):
|
||||
def pick(n; m): # pick n, from m onwards
|
||||
if n == 0 then []
|
||||
elif m == length then empty
|
||||
elif n == 1 then (.[m:][] | [.])
|
||||
else ([.[m]] + pick(n-1; m)), pick(n; m+1)
|
||||
end;
|
||||
pick(n;0) ;
|
||||
|
||||
# Given any array, produce an array of [item, count] pairs for each run.
|
||||
def runs:
|
||||
reduce .[] as $item
|
||||
( [];
|
||||
if . == [] then [ [ $item, 1] ]
|
||||
else .[length-1] as $last
|
||||
| if $last[0] == $item then (.[0:length-1] + [ [$item, $last[1] + 1] ] )
|
||||
else . + [[$item, 1]]
|
||||
end
|
||||
end ) ;
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
def terminus:
|
||||
# sum of the squared digits
|
||||
def ssdigits: tostring | explode | map(. - 48 | .*.) | add;
|
||||
|
||||
if . == 1 or . == 89 then .
|
||||
else ssdigits | terminus
|
||||
end;
|
||||
|
||||
# Count the number of integers i in [1... 10^D] with terminus equal to 89.
|
||||
def task(D):
|
||||
# The max sum of squares is D*81 so return an array that will instantly
|
||||
# reveal whether n|terminus is 89:
|
||||
def cache:
|
||||
reduce range(1; D*81+1) as $d ([false]; . + [$d|terminus == 89]);
|
||||
|
||||
# Compute n / (i1! * i2! * ... ) for the given combination,
|
||||
# which is assumed to be in order:
|
||||
def combinations(n):
|
||||
runs | map( .[1] | factorial) | reduce .[] as $i (n; ./$i);
|
||||
|
||||
cache as $cache
|
||||
| (D|factorial) as $Dfactorial
|
||||
| reduce ([range(0;10)] | pick(D)) as $digits
|
||||
(0;
|
||||
($digits | map(.*.) | add) as $ss
|
||||
| if $cache[$ss] then . + ($digits|combinations($Dfactorial))
|
||||
else .
|
||||
end) ;
|
||||
|
|
@ -0,0 +1 @@
|
|||
task(8)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
$ jq -M -n -f Iterated_digits_squaring_using_pick.jq
|
||||
85744333
|
||||
|
||||
# Using jq>1.4:
|
||||
# user 0m2.595s
|
||||
# sys 0m0.010s
|
||||
|
||||
# Using jq 1.4:
|
||||
# user 0m3.942s
|
||||
# sys 0m0.009s
|
||||
Loading…
Add table
Add a link
Reference in a new issue