Numbers divisible by their individual digits, but not by the product of their digits.: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(Add Factor)
Line 7: Line 7:





=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: combinators.short-circuit grouping kernel math
math.functions math.ranges math.text.utils prettyprint sequences ;

: needle? ( n -- ? )
dup 1 digit-groups dup product
{
[ 2nip zero? not ]
[ nip divisor? not ]
[ drop [ divisor? ] with all? ]
} 3&& ;

1000 [1..b] [ needle? ] filter 9 group simple-table.</lang>
{{out}}
<pre>
22 33 44 48 55 66 77 88 99
122 124 126 155 162 168 184 222 244
248 264 288 324 333 336 366 396 412
424 444 448 488 515 555 636 648 666
728 777 784 824 848 864 888 936 999
</pre>


=={{header|Julia}}==
=={{header|Julia}}==

Revision as of 08:09, 8 April 2021

Numbers divisible by their individual digits, but not by the product of their digits. is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find and show numbers divisible by their individual digits, but not by the product of their digits, where n < 1000



Factor

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: combinators.short-circuit grouping kernel math math.functions math.ranges math.text.utils prettyprint sequences ;

needle? ( n -- ? )
   dup 1 digit-groups dup product
   {
       [ 2nip zero? not ]
       [ nip divisor? not ]
       [ drop [ divisor? ] with all? ]
   } 3&& ;

1000 [1..b] [ needle? ] filter 9 group simple-table.</lang>

Output:
22  33  44  48  55  66  77  88  99
122 124 126 155 162 168 184 222 244
248 264 288 324 333 336 366 396 412
424 444 448 488 515 555 636 648 666
728 777 784 824 848 864 888 936 999

Julia

<lang julia>isonlydigdivisible(n) = (d = digits(n); !(0 in d) && all(x -> n % x == 0, d) && n % prod(d) != 0)

foreach(p -> print(rpad(p[2], 5), p[1] % 15 == 0 ? "\n" : ""), enumerate(filter(isonlydigdivisible, 1:1000)))

</lang>

Output:
22   33   44   48   55   66   77   88   99   122  124  126  155  162  168  
184  222  244  248  264  288  324  333  336  366  396  412  424  444  448
488  515  555  636  648  666  728  777  784  824  848  864  888  936  999


Ring

<lang ring> load "stdlib.ring"

decimals(0) see "working..." + nl see "Numbers divisible by their individual digits, but not by the product of their digits are:" + nl

row = 0 limit = 1000

for n = 1 to limit

   flag = 1
   pro = 1
   strn = string(n)
   for m = 1 to len(strn)
       temp = strn[m]
       if temp != 0
          pro = pro * number(temp)
       ok
       if n%temp = 0
          flag = 1
       else
          flag = 0
          exit
       ok
    next
    bool = ((n%pro) != 0)
    if flag = 1 and bool
       row = row + 1
       see "" + n + " "
       if row%10 = 0
          see nl
       ok
    ok

next

see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>

Output:
working...
Numbers divisible by their individual digits, but not by the product of their digits are:
22 33 44 48 55 66 77 88 99 122 
124 126 155 162 168 184 222 244 248 264 
288 324 333 336 366 396 412 424 444 448 
488 515 555 636 648 666 728 777 784 824 
848 864 888 936 999 
Found 45 numbers
done...