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,31 @@
USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-mdr ( n -- )
dup [ 1array ] dip mdr append
"%-12d has multiplicative persistence %d and MDR %d.\n"
vprintf ;
: first5 ( n -- seq ) ! first 5 numbers with MDR of n
0 lfrom swap '[ mdr second _ = ] lfilter 5 swap ltake list>array ;
: print-first5 ( i n -- )
"%-5d" printf bl first5 [ "%-5d " printf ] each nl ;
: header ( -- )
"MDR | First five numbers with that MDR" print
"--------------------------------------" print ;
: first5-table ( -- )
header 10 iota [ print-first5 ] each-index ;
: main ( -- )
{ 123321 7739 893 899998 } [ print-mdr ] each nl first5-table ;
MAIN: main

View file

@ -0,0 +1,63 @@
# Project : Digital root/Multiplicative digital root
# Date : 2017/11/21
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
root = newlist(10, 5)
for r = 1 to 10
for x = 1 to 5
root[r][x] = 0
next
next
root2 = list(10)
for y = 1 to 10
root2[y] = 0
next
see "Number MDR MP" + nl
num = [123321, 7739, 893, 899998]
digroot(num)
see nl
num = 0:12000
digroot(num)
see "First five numbers with MDR in first column:" + nl
for n1 = 1 to 10
see "" + (n1-1) + " => "
for n2 = 1 to 5
see "" + root[n1][n2] + " "
next
see nl
next
func digroot(num)
for n = 1 to len(num)
sum = 0
numold = num[n]
while true
pro = 1
strnum = string(numold)
for nr = 1 to len(strnum)
pro = pro * number(strnum[nr])
next
sum = sum + 1
numold = pro
numn = string(num[n])
sp = 6 - len(string(num[n]))
if sp > 0
for p = 1 to sp + 2
numn = " " + numn
next
ok
if len(string(numold)) = 1 and len(num) < 5
see "" + numn + " " + numold + " " + sum + nl
exit
ok
if len(string(numold)) = 1 and len(num) > 4
root2[numold+1] = root2[numold+1] + 1
if root2[numold+1] < 6
root[numold+1][root2[numold+1]] = num[n]
ok
exit
ok
end
next