Numbers divisible by their individual digits, but not by the product of their digits.: Difference between revisions
Numbers divisible by their individual digits, but not by the product of their digits. (view source)
Revision as of 00:02, 28 August 2022
, 1 year agosyntax highlighting fixup automation
(→{{header|J}}: redo, with some comments) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9:
{{trans|Python}}
<
‘True if n is divisible by each of its digits,
but not divisible by the product of those digits.
Line 28:
V w = xs.last.len
print(xs.len" matching numbers:\n")
print(chunksOf(10)(xs).map(row -> row.map(cell -> cell.rjust(:w, ‘ ’)).join(‘ ’)).join("\n"))</
{{out}}
Line 42:
=={{header|8086 Assembly}}==
<
org 100h
section .text
Line 93:
section .data
db '*****'
dbuf: db 13,10,'$'</
{{out}}
Line 144:
=={{header|Action!}}==
<
BYTE d
INT tmp,prod
Line 172:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_divisible_by_their_individual_digits,_but_not_by_the_product_of_their_digits.png Screenshot from Atari 8-bit computer]
Line 181:
=={{header|Ada}}==
<
with Ada.Integer_Text_Io;
Line 219:
end if;
end loop;
end Numbers_Divisible;</
{{out}}
<pre> 22 33 44 48 55 66 77 88 99 122 124 126 155 162 168
Line 226:
=={{header|ALGOL 68}}==
<
INT max number = 999;
INT number count := 0;
Line 250:
FI
OD
END</
{{out}}
<pre>
Line 259:
=={{header|ALGOL-M}}==
<
integer function mod(a, b);
integer a, b;
Line 298:
end;
write("");
end</
{{out}}
<pre> 22 33 44 48 55 66 77 88 99 122
Line 307:
=={{header|ALGOL W}}==
<
% returns true if n is divisible by its digits but not the product of its %
% digits, false otherwise %
Line 341:
end if_divisibleByDigitsButNotDigitProduct__i
end for_i
end.</
{{out}}
<pre>
Line 352:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{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
Line 358:
=={{header|Arturo}}==
<
digs: digits n
facts: factors n
Line 365:
]
print select 1..999 => valid?</
{{out}}
Line 372:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">main:
while n < 1000
{
Line 388:
}
MsgBox % result
</syntaxhighlight>
{{out}}
<pre>22 33 44 48 55 66 77 88 99 122 124 126
Line 396:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_DIVISIBLE_BY_THEIR_INDIVIDUAL_DIGITS_BUT_NOT_BY_THE_PRODUCT_OF_THEIR_DIGITS.AWK
# converted from C
Line 419:
return(n % p)
}
</syntaxhighlight>
{{out}}
<pre>
Line 431:
=={{header|BASIC}}==
<
20 FOR I=1 TO 999
30 N=I: P=1
Line 441:
90 IF N THEN 40
100 IF I MOD P <> 0 THEN PRINT I,
110 NEXT I</
{{out}}
<pre> 22 33 44 48 55
Line 454:
=={{header|BCPL}}==
<
let divisible(n) = valof
Line 478:
$)
wrch('*N')
$)</
{{out}}
<pre> 22 33 44 48 55 66 77 88 99 122
Line 487:
=={{header|C}}==
<
int divisible(int n) {
Line 514:
return 0;
}</
{{out}}
Line 525:
=={{header|CLU}}==
<
prod: int := 1
dgts: int := n
Line 549:
end
end
end start_up</
{{out}}
<pre> 22 33 44 48 55 66 77 88 99 122
Line 558:
=={{header|COBOL}}==
<
PROGRAM-ID. DIV-BY-DGTS-BUT-NOT-PROD.
Line 607:
MULTIPLY DIGIT(D) BY NDIV.
IF NDIV IS NOT EQUAL TO N SET OK TO 0.
NOPE. EXIT.</
{{out}}
<pre style='height: 50ex;'> 22
Line 656:
=={{header|Cowgol}}==
<
sub divisible(n: uint16): (r: uint8) is
Line 692:
n := n + 1;
end loop;
print_nl();</
{{out}}
Line 703:
=={{header|Draco}}==
<
word dprod, c, dgt;
bool div;
Line 733:
fi
od
corp</
{{out}}
<pre> 22 33 44 48 55 66 77 88 99 122
Line 742:
=={{header|F_Sharp|F#}}==
<
// Nigel Galloway. April 9th., 2021
let rec fN i g e l=match g%10,g/10 with (0,_)->false |(n,_) when i%n>0->false |(n,0)->i%(l*n)>0 |(n,g)->fN i g (e+n) (l*n)
seq{1..999}|>Seq.filter(fun n->fN n n 0 1)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 754:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.functions math.ranges math.text.utils prettyprint sequences ;
Line 765:
} 3&& ;
1000 [1..b] [ needle? ] filter 9 group simple-table.</
{{out}}
<pre>
Line 776:
=={{header|FOCAL}}==
<
01.20 Q
Line 792:
02.75 S Z=I/P
02.80 I (FITR(Z)-Z)2.85,2.65
02.85 T %4,I,!</
{{out}}
Line 844:
=={{header|Forth}}==
{{works with|Gforth}}
<
1 { p }
n
Line 872:
main
bye</
{{out}}
Line 885:
=={{header|FreeBASIC}}==
This function does a bit more than the task asks for, just to make things interesting.
<
'returns 1 if the number is divisible by its digits
' 2 if it is NOT divisible by the product of its digits
Line 903:
for i as uinteger = 1 to 999
if divdignp(i) = 3 then print i;" ";
next i : print</
{{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
Line 911:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 947:
}
fmt.Printf("\n%d such numbers found\n", len(res))
}</
{{out}}
Line 962:
=={{header|Haskell}}==
<
import Text.Printf
Line 981:
where
n = takeWhile (< 1000) numbers
split = chunksOf 10 n</
{{out}}
Line 993:
and another approach might be to obtain (unordered) digit lists numerically, rather than by string conversion.
<
import Data.List (unfoldr)
import Data.List.Split (chunksOf)
Line 1,029:
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</
{{Out}}
<pre> 22 33 44 48 55 66 77 88 99 122
Line 1,038:
=={{header|J}}==
<
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</
<code>([ #~ ... ) >:i.999</code> filters the numbers based on the predicate (shown as '...' here).
Line 1,050:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<
tostring | explode | map( [.] | implode | tonumber);
Line 1,062:
| digits
| all( unique[]; $n % . == 0)
and ($n % prod != 0);</
'''The Task'''
<
(range(1; 1000)
| select(is_divisible_by_digits_but_not_product))</
{{out}}
<pre>
Line 1,119:
=={{header|Julia}}==
<
foreach(p -> print(rpad(p[2], 5), p[1] % 15 == 0 ? "\n" : ""), enumerate(filter(isonlydigdivisible, 1:1000)))
</
<pre>
22 33 44 48 55 66 77 88 99 122 124 126 155 162 168
Line 1,130:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 1,164:
(( ! i % 10 )) || _isdivisible ${i} || printf "%d " ${i}
done
</syntaxhighlight>
{{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|MAD}}==
<
PRINT COMMENT $ $
Line 1,191:
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </
{{out}}
<pre style="height: 50ex;"> 22
Line 1,240:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SaveDivisible[n_,0] := False
SaveDivisible[n_,m_] := Divisible[n,m]
DivisibleDigits[n_Integer] := AllTrue[IntegerDigits[n],SaveDivisible[n,#]&]
Select[Range[999],DivisibleDigits[#]\[And]!SaveDivisible[#,Times@@IntegerDigits[#]]&]
Length[%]</
{{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}
Line 1,251:
=={{header|Nim}}==
<
iterator digits(n: Positive): int =
Line 1,270:
echo "Found ", result.len, " matching numbers."
for i, n in result:
stdout.write ($n).align(3), if (i + 1) mod 9 == 0: '\n' else: ' '</
{{out}}
Line 1,282:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<
program DivByDgtsNotByProdOfDgts;
Line 1,325:
writeln;
writeln(' count : ',cnt);
END.</
{{out}}
<pre>
Line 1,335:
=={{header|Perl}}==
<
use strict;
Line 1,346:
} 1 .. 999;
print @numbers . " numbers found\n\n@numbers\n" =~ s/.{25}\K /\n/gr;</
{{out}}
<pre>
Line 1,361:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">didbntp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
Line 1,374:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">didbntp</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d didbntp thingies less than one thousand: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">","</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 1,381:
=={{header|PL/M}}==
<
/* CHECK NUMBER */
Line 1,426:
CALL BDOS(0,0);
EOF</
{{out}}
<pre style="height:50ex;">22
Line 1,475:
=={{header|Plain English}}==
<
Start up.
Loop.
Line 1,496:
Repeat.
If the number is evenly divisible by the digit product, say no.
Say yes.</
{{out}}
<pre>
Line 1,503:
=={{header|Python}}==
<
from functools import reduce
Line 1,555:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>45 matching numbers:
Line 1,567:
=={{header|Quackery}}==
<
[ 2drop false ] done
mod 0 = ] is divisible ( n n --> b )
Line 1,589:
drop ] is meetscriteria ( n n --> b )
1000 times [ i^ meetscriteria if [ i^ echo sp ] ]</
{{out}}
Line 1,597:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
(^1000).grep: -> $n { $n.contains(0) ?? False !! all |($n.comb).map($n %% *), $n % [*] $n.comb };</
{{out}}
Line 1,609:
=={{header|REXX}}==
<
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 1,641:
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 ?</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,657:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,695:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,711:
=={{header|Sidef}}==
<
n.digits.all {|d| d `divides` n } && !(n.digits.prod `divides` n)
}.say</
{{out}}
<pre>
Line 1,720:
=={{header|Snobol}}==
<
divis p = 1
i = n
Line 1,732:
loop output = divis(n) n
n = lt(n,1000) n + 1 :s(loop)
end</
{{out}}
Line 1,786:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/seq" for Lst
import "/fmt" for Fmt
Line 1,800:
System.print("Numbers < 1000 divisible by their digits, but not by the product thereof:")
for (chunk in Lst.chunks(res, 9)) Fmt.print("$4d", chunk)
System.print("\n%(res.count) such numbers found")</
{{out}}
Line 1,815:
=={{header|XPL0}}==
<
\Return 'true' if N is divisible by its digits and not by the product of its digits
int N, M, Digit, Product;
Line 1,841:
Text(0, " such integers found below 1000.
");
]</
{{out}}
|