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,22 @@
|
|||
PROGRAM NARCISISTIC
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
BEGIN
|
||||
N=0
|
||||
LOOP
|
||||
LENG=LEN(MID$(STR$(N),2))
|
||||
SUM=0
|
||||
FOR I=1 TO LENG DO
|
||||
C$=MID$(STR$(N),2)
|
||||
C=VAL(MID$(C$,I,1))
|
||||
SUM+=C^LENG
|
||||
END FOR
|
||||
IF N=SUM THEN
|
||||
PRINT(N;)
|
||||
COUNT=COUNT+1
|
||||
EXIT IF COUNT=25
|
||||
END IF
|
||||
N=N+1
|
||||
END LOOP
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
' normal version: 17-06-2015
|
||||
' compile with: fbc -s console
|
||||
' can go up to 19 digits (ulongint is 64bit), above 19 overflow will occur
|
||||
|
||||
Dim As Integer n, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, a, b
|
||||
Dim As Integer d()
|
||||
Dim As ULongInt d2pow(0 To 9) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
Dim As ULongInt x
|
||||
Dim As String str_x
|
||||
|
||||
For n = 1 To 7
|
||||
For n9 = n To 0 Step -1
|
||||
For n8 = n-n9 To 0 Step -1
|
||||
For n7 = n-n9-n8 To 0 Step -1
|
||||
For n6 = n-n9-n8-n7 To 0 Step -1
|
||||
For n5 = n-n9-n8-n7-n6 To 0 Step -1
|
||||
For n4 = n-n9-n8-n7-n6-n5 To 0 Step -1
|
||||
For n3 = n-n9-n8-n7-n6-n5-n4 To 0 Step -1
|
||||
For n2 = n-n9-n8-n7-n6-n5-n4-n3 To 0 Step -1
|
||||
For n1 = n-n9-n8-n7-n6-n5-n4-n3-n2 To 0 Step -1
|
||||
n0 = n-n9-n8-n7-n6-n5-n4-n3-n2-n1
|
||||
|
||||
x = n1*d2pow(1) + n2*d2pow(2) + n3*d2pow(3) + n4*d2pow(4) + n5*d2pow(5)_
|
||||
+ n6*d2pow(6) + n7*d2pow(7) + n8*d2pow(8) + n9*d2pow(9)
|
||||
|
||||
str_x = Str(x)
|
||||
If Len(str_x) = n Then
|
||||
|
||||
ReDim d(10)
|
||||
For a = 0 To n-1
|
||||
d(Str_x[a]- Asc("0")) += 1
|
||||
Next a
|
||||
|
||||
If n0 = d(0) AndAlso n1 = d(1) AndAlso n2 = d(2) AndAlso n3 = d(3)_
|
||||
AndAlso n4 = d(4) AndAlso n5 = d(5) AndAlso n6 = d(6)_
|
||||
AndAlso n7 = d(7) AndAlso n8 = d(8) AndAlso n9 = d(9) Then
|
||||
Print x
|
||||
End If
|
||||
End If
|
||||
|
||||
Next n1
|
||||
Next n2
|
||||
Next n3
|
||||
Next n4
|
||||
Next n5
|
||||
Next n6
|
||||
Next n7
|
||||
Next n8
|
||||
Next n9
|
||||
|
||||
For a As Integer = 2 To 9
|
||||
d2pow(a) = d2pow(a) * a
|
||||
Next a
|
||||
|
||||
Next n
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
' gmp version: 17-06-2015
|
||||
' uses gmp
|
||||
' compile with: fbc -s console
|
||||
|
||||
#Include Once "gmp.bi"
|
||||
' change the number after max for the maximum n-digits you want (2 to 61)
|
||||
#Define max 61
|
||||
|
||||
Dim As Integer n, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
|
||||
Dim As Integer i, j
|
||||
Dim As UInteger d()
|
||||
Dim As ZString Ptr gmp_str
|
||||
gmp_str = Allocate(100)
|
||||
|
||||
' create gmp integer array,
|
||||
Dim d2pow(9, max) As Mpz_ptr
|
||||
' initialize array and set start value,
|
||||
For i = 0 To 9
|
||||
For j = 0 To max
|
||||
d2pow(i, j) = Allocate(Len(__mpz_struct)) : Mpz_init(d2pow(i, j))
|
||||
Next j
|
||||
Next i
|
||||
|
||||
' gmp integers for to hold intermediate result
|
||||
Dim As Mpz_ptr x1 = Allocate(Len(__mpz_struct)) : Mpz_init(x1)
|
||||
Dim As Mpz_ptr x2 = Allocate(Len(__mpz_struct)) : Mpz_init(x2)
|
||||
Dim As Mpz_ptr x3 = Allocate(Len(__mpz_struct)) : Mpz_init(x3)
|
||||
Dim As Mpz_ptr x4 = Allocate(Len(__mpz_struct)) : Mpz_init(x4)
|
||||
Dim As Mpz_ptr x5 = Allocate(Len(__mpz_struct)) : Mpz_init(x5)
|
||||
Dim As Mpz_ptr x6 = Allocate(Len(__mpz_struct)) : Mpz_init(x6)
|
||||
Dim As Mpz_ptr x7 = Allocate(Len(__mpz_struct)) : Mpz_init(x7)
|
||||
Dim As Mpz_ptr x8 = Allocate(Len(__mpz_struct)) : Mpz_init(x8)
|
||||
|
||||
For n = 1 To max
|
||||
|
||||
For i = 1 To 9
|
||||
'Mpz_set_ui(d2pow(i,0), 0)
|
||||
Mpz_ui_pow_ui(d2pow(i,1), i, n)
|
||||
For j = 2 To n
|
||||
Mpz_mul_ui(d2pow(i, j), d2pow(i, 1), j)
|
||||
Next j
|
||||
Next i
|
||||
|
||||
For n9 = n To 0 Step -1
|
||||
For n8 = n-n9 To 0 Step -1
|
||||
Mpz_add(x8, d2pow(9, n9), d2pow(8, n8))
|
||||
For n7 = n-n9-n8 To 0 Step -1
|
||||
Mpz_add(x7, x8, d2pow(7, n7))
|
||||
For n6 = n-n9-n8-n7 To 0 Step -1
|
||||
Mpz_add(x6, x7, d2pow(6, n6))
|
||||
For n5 = n-n9-n8-n7-n6 To 0 Step -1
|
||||
Mpz_add(x5, x6, d2pow(5, n5))
|
||||
For n4 = n-n9-n8-n7-n6-n5 To 0 Step -1
|
||||
Mpz_add(x4, x5, d2pow(4, n4))
|
||||
For n3 = n-n9-n8-n7-n6-n5-n4 To 0 Step -1
|
||||
Mpz_add(x3, x4, d2pow(3, n3))
|
||||
For n2 = n-n9-n8-n7-n6-n5-n4-n3 To 0 Step -1
|
||||
Mpz_add(x2, x3, d2pow(2, n2))
|
||||
For n1 = n-n9-n8-n7-n6-n5-n4-n3-n2 To 0 Step -1
|
||||
Mpz_add_ui(x1, x2, n1)
|
||||
n0 = n-n9-n8-n7-n6-n5-n4-n3-n2-n1
|
||||
|
||||
Mpz_get_str(gmp_str, 10, x1)
|
||||
|
||||
If Len(*gmp_str) = n Then
|
||||
ReDim d(10)
|
||||
|
||||
For i = 0 To n-1
|
||||
d(gmp_str[i] - Asc("0")) += 1
|
||||
Next i
|
||||
|
||||
If n9 = d(9) AndAlso n8 = d(8) AndAlso n7 = d(7) AndAlso n6 = d(6)_
|
||||
AndAlso n5 = d(5) AndAlso n4 = d(4) AndAlso n3 = d(3)_
|
||||
AndAlso n2 = d(2) AndAlso n1 = d(1) AndAlso n0 = d(0) Then
|
||||
Print *gmp_str
|
||||
End If
|
||||
ElseIf Len(*gmp_str) < n Then
|
||||
' all for next loops have a negative step value
|
||||
' if len(str_x) becomes smaller then n it's time to try the next n value
|
||||
' GoTo label1 ' old school BASIC
|
||||
' prefered FreeBASIC style
|
||||
Exit For, For, For, For, For, For, For, For, For
|
||||
' leave n1, n2, n3, n4, n5, n6, n7, n8, n9 loop
|
||||
' and continue's after next n9
|
||||
End If
|
||||
|
||||
Next n1
|
||||
Next n2
|
||||
Next n3
|
||||
Next n4
|
||||
Next n5
|
||||
Next n6
|
||||
Next n7
|
||||
Next n8
|
||||
Next n9
|
||||
' label1:
|
||||
Next n
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
def narcissistic( start ) =
|
||||
power = 1
|
||||
powers = array( 0..9 )
|
||||
|
||||
def narc( n ) =
|
||||
num = n.toString()
|
||||
m = num.length()
|
||||
|
||||
if power != m
|
||||
power = m
|
||||
powers( 0..9 ) = [i^m | i <- 0..9]
|
||||
|
||||
if n == sum( powers(int(d)) | d <- num )
|
||||
n # narc( n + 1 )
|
||||
else
|
||||
narc( n + 1 )
|
||||
|
||||
narc( start )
|
||||
|
||||
println( narcissistic(0).take(25) )
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
: isNarcissistic(n)
|
||||
| i m |
|
||||
n 0 while( n ) [ n 10 /mod ->n swap 1 + ] ->m
|
||||
0 m loop: i [ swap m pow + ] == ;
|
||||
|
||||
: genNarcissistic(n)
|
||||
| l |
|
||||
ListBuffer new dup ->l
|
||||
0 while(l size n <>) [ dup isNarcissistic ifTrue: [ dup l add ] 1 + ] drop ;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
n = 0
|
||||
count = 0
|
||||
size = 15
|
||||
while count != size
|
||||
m = isNarc(n)
|
||||
if m=1 see "" + n + " is narcisstic" + nl
|
||||
count = count + 1 ok
|
||||
n = n + 1
|
||||
end
|
||||
|
||||
func isNarc n
|
||||
m = len(string(n))
|
||||
sum = 0
|
||||
digit = 0
|
||||
for pos = 1 to m
|
||||
digit = number(substr(string(n), pos, 1))
|
||||
sum = sum + pow(digit,m)
|
||||
next
|
||||
nr = (sum = n)
|
||||
return nr
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
func is_narcissistic(n) {
|
||||
n.digits »**» n.len -> sum(0) == n
|
||||
}
|
||||
|
||||
var count = 0
|
||||
for i in (0..^Inf) {
|
||||
if (is_narcissistic(i)) {
|
||||
say "#{++count}\t#{i}"
|
||||
break if (count == 25)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def is_narcissistic:
|
||||
def digits: tostring | explode[] | [.] | implode | tonumber;
|
||||
def pow(n): . as $x | reduce range(0;n) as $i (1; . * $x);
|
||||
|
||||
(tostring | length) as $len
|
||||
| . == reduce digits as $d (0; . + ($d | pow($len)) )
|
||||
end;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Input: [i, [0^i, 1^i, 2^i, ..., 9^i]]
|
||||
# Output: [j, [0^j, 1^j, 2^j, ..., 9^j]]
|
||||
# provided j is i or (i+1)
|
||||
def powers(j):
|
||||
if .[0] == j then .
|
||||
else .[0] += 1
|
||||
| reduce range(0;10) as $k (.; .[1][$k] *= $k)
|
||||
end;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Input: [n, [i, [0^i, 1^i, 2^i,...]]] where i is the number of digits in n.
|
||||
def is_narcissistic:
|
||||
def digits: tostring | explode[] | [.] | implode | tonumber;
|
||||
.[1][1] as $powers
|
||||
| .[0]
|
||||
| if . < 0 then false
|
||||
else . == reduce digits as $d (0; . + $powers[$d] )
|
||||
end;
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
# If your jq has "while", then feel free to omit the following definition:
|
||||
def while(cond; update):
|
||||
def _while: if cond then ., (update | _while) else empty end;
|
||||
_while;
|
||||
|
||||
# The first k narcissistic numbers, beginning with 0:
|
||||
def narcissistic(k):
|
||||
# State: [n, is_narcissistic, count, [len, [0^len, 1^len, ...]]]
|
||||
# where len is the number of digits in n.
|
||||
[0, true, 1, [1, [range(0;10)]]]
|
||||
| while( .[2] <= k;
|
||||
.[3] as $powers
|
||||
| (.[0]+1) as $n
|
||||
| ($n | tostring | length) as $len
|
||||
| ($powers | powers($len)) as $powersprime
|
||||
| if [$n, $powersprime] | is_narcissistic
|
||||
then [$n, true, .[2] + 1, $powersprime]
|
||||
else [$n, false, .[2], $powersprime ]
|
||||
end )
|
||||
| select(.[1])
|
||||
| "\(.[2]): \(.[0])" ;
|
||||
|
||||
narcissistic(25)
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
jq -r -n -f Narcissitic_decimal_number.jq
|
||||
1: 0
|
||||
2: 1
|
||||
3: 2
|
||||
4: 3
|
||||
5: 4
|
||||
6: 5
|
||||
7: 6
|
||||
8: 7
|
||||
9: 8
|
||||
10: 9
|
||||
11: 153
|
||||
12: 370
|
||||
13: 371
|
||||
14: 407
|
||||
15: 1634
|
||||
16: 8208
|
||||
17: 9474
|
||||
18: 54748
|
||||
19: 92727
|
||||
20: 93084
|
||||
21: 548834
|
||||
22: 1741725
|
||||
23: 4210818
|
||||
24: 9800817
|
||||
25: 9926315
|
||||
Loading…
Add table
Add a link
Reference in a new issue