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

From Rosetta Code
Content added Content deleted
(Add J)
(Add BASIC)
Line 57: Line 57:
<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
<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>
636 648 666 728 777 784 824 848 864 888 936 999</pre>

=={{header|BASIC}}==
<lang basic>10 DEFINT A-Z
20 FOR I=1 TO 999
30 N=I: P=1
40 D=N MOD 10
50 IF D=0 THEN 110
60 P=P*D
70 IF I MOD D THEN 110
80 N=N\10
90 IF N THEN 40
100 IF I MOD P <> 0 THEN PRINT I,
110 NEXT I</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|C}}==
=={{header|C}}==

Revision as of 04:20, 9 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 positive decimal integers divisible by their individual digits,   but not divisible by the product of their digits,
where   n   <   1000

Ada

<lang Ada>with Ada.Text_Io; with Ada.Integer_Text_Io;

procedure Numbers_Divisible is

  function Is_Divisible (N : Natural) return Boolean is
     function To_Decimal (C : Character) return Natural
     is ( Character'Pos (C) - Character'Pos ('0'));
     Image : constant String := N'Image;
     Digit : Natural;
     Prod  : Natural := 1;
  begin
     for A in Image'First + 1 .. Image'Last loop
        Digit := To_Decimal (Image (A));
        if Digit = 0 then
           return False;
        end if;
        if N mod Digit /= 0 then
           return False;
        end if;
        Prod := Prod * Digit;
     end loop;
     return N mod Prod /= 0;
  end Is_Divisible;
  Count : Natural := 0;

begin

  for N in 1 .. 999 loop
     if Is_Divisible (N) then
        Count := Count + 1;
        Ada.Integer_Text_Io.Put (N, Width => 5);
        if Count mod 15 = 0 then
           Ada.Text_Io.New_Line;
        end if;
     end if;
  end loop;

end Numbers_Divisible;</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

APL

Works with: Dyalog APL

<lang APL>(⊢(/⍨)((⍎¨∘⍕)((∧/0=|)∧0≠(×/⊣)|⊢)⊢)¨)⍳999</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

BASIC

<lang basic>10 DEFINT A-Z 20 FOR I=1 TO 999 30 N=I: P=1 40 D=N MOD 10 50 IF D=0 THEN 110 60 P=P*D 70 IF I MOD D THEN 110 80 N=N\10 90 IF N THEN 40 100 IF I MOD P <> 0 THEN PRINT I, 110 NEXT I</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

C

<lang c>#include <stdio.h>

int divisible(int n) {

   int p = 1;
   int c, d;
   
   for (c=n; c; c /= 10) {
       d = c % 10;
       if (!d || n % d) return 0;
       p *= d;
   }
   
   return n % p;

}

int main() {

   int n, c=0;
   
   for (n=1; n<1000; n++) {
       if (divisible(n)) {
           printf("%5d", n);
           if (!(++c % 10)) printf("\n");
       }
   }
   printf("\n");
   
   return 0;

}</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

Cowgol

<lang cowgol>include "cowgol.coh";

sub divisible(n: uint16): (r: uint8) is

   var product: uint16 := 1;
   var c := n;
   r := 1;
   
   while c != 0 loop
       var digit := c % 10;
       if digit == 0 or n % digit != 0 then
           r := 0;
           return;
       end if;
       product := product * digit;
       c := c / 10;
   end loop;
   
   if n % product == 0 then
       r := 0;
   end if;

end sub;

var n: uint16 := 1; var c: uint8 := 1; while n < 1000 loop

   if divisible(n) != 0 then
       print_i16(n);
       c := c + 1;
       if c % 10 == 1 then
           print_nl();
       else
           print_char('\t');
       end if;
   end if;
   n := n + 1;

end loop; print_nl();</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

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

Haskell

<lang haskell>import Control.Monad import Data.List.Split import Text.Printf

divisible :: Int -> Bool divisible = divdgt `ap` dgt

   where dgt        = map (read . pure) . show
         divdgt x d = (not $ elem 0 d)
                   && 0/=x `mod` product d 
                   && all ((0==).mod x) d
         

numbers :: [Int] numbers = filter divisible [1..]

main :: IO () main = putStr $ unlines $ map (concatMap $ printf "%5d") $ split

   where n      = takeWhile (< 1000) numbers
         split  = chunksOf 10 n</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

J

<lang J>([ #~ ((10 #.^:_1]) ((0:~:*/@[|]) *. *./@(0:=|)) ])"0) >:i.999</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

Phix

function didbntp(integer n)
    integer w = n, p = 1
    while w do
        integer d = remainder(w,10)
        if d=0 or remainder(n,d) then return false end if
        p *= d
        w = floor(w/10)
    end while
    return remainder(n,p)!=0
end function
sequence res = apply(filter(tagset(1000),didbntp),sprint)
printf(1,"found %d didbntp thingies less than one thousand: %s\n",{length(res),join(shorten(res,"",5),",")})
Output:
found 45 didbntp thingies less than one thousand: 22,33,44,48,55,...,848,864,888,936,999

Raku

<lang perl6>say "{+$_} matching numbers:\n{.batch(10)».fmt('%3d').join: "\n"}" given

  (^1000).grep: -> $n { $n.contains(0) ?? False !! all |($n.comb).map($n %% *), $n % [*] $n.comb };</lang>
Output:
45 matching numbers:
 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

REXX

<lang rexx>/*REXX pgm finds numbers divisible by its individual digits, but not by product of digs.*/ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */

                   @ndnp= ' base ten integers  < '   commas(hi)   " that are divisible" ,
                          'by its digits, but not by the product of its digits'

if cols>0 then say ' index │'center(@ndnp, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') finds= 0; idx= 1 /*initialize # of found numbers & index*/ $= /*a list of integers found (so far). */

    do j=1  for hi-1;    L= length(j);    != 1  /*search for integers within the range.*/
    if pos(0, j)>0  then iterate                /*Does J have a zero?  Yes, then skip. */
           do k=1  for L;    x= substr(j, k, 1) /*extract a single decimal digit from J*/
           if j//x\==0   then iterate j         /*J ÷ by this digit?  No, then skip it.*/
           != ! * x                             /*compute the running product of digits*/
           end   /*k*/
    if j//!==0           then iterate           /*J ÷ by its digit product?  Yes, skip.*/
    finds= finds + 1                            /*bump the number of  found  integers. */
    if cols==0           then iterate           /*Build the list  (to be shown later)? */
    $= $ right( commas(j), w)                   /*add the number found to the  $  list.*/
    if finds//cols\==0   then iterate           /*have we populated a line of output?  */
    say center(idx, 7)'│'  substr($, 2);   $=   /*display what we have so far  (cols). */
    idx= idx + cols                             /*bump the  index  count for the output*/
    end   /*j*/

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(finds) @ndnp exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>

output   when using the default inputs:
 index │      base ten integers  <  1,000  that are divisible by its digits, but not by the product of its digits
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │         22         33         44         48         55         66         77         88         99        122
  11   │        124        126        155        162        168        184        222        244        248        264
  21   │        288        324        333        336        366        396        412        424        444        448
  31   │        488        515        555        636        648        666        728        777        784        824
  41   │        848        864        888        936        999
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Found  45  base ten integers  <  1,000  that are divisible by its digits, but not by the product of its digits

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...