Sum of the digits of n is substring of n: Difference between revisions

Add Refal
(Add Miranda)
(Add Refal)
 
(7 intermediate revisions by 6 users not shown)
Line 810:
{{out}}
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900 910 911 912 913 914 915 916 917 918 919</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
{This code would normally be in a library, but is included here for clarity}
 
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
{Numbers returned from least to most significant}
var T,I,DC: integer;
begin
DC:=Trunc(Log10(N))+1;
SetLength(IA,DC);
for I:=0 to DC-1 do
begin
T:=N mod 10;
N:=N div 10;
IA[I]:=T;
end;
end;
 
 
procedure SumDigitsSubstring(Memo: TMemo);
var N,J,Cnt,Sum: integer;
var Dg: TIntegerDynArray;
var NS,SS,S: string;
begin
S:='';
Cnt:=0;
for N:=0 to 1000-1 do
begin
GetDigits(N,Dg);
Sum:=0;
for J:=0 to High(Dg) do
Sum:=Sum+Dg[J];
NS:=IntToStr(N);
SS:=IntToStr(Sum);
if Pos(SS,NS)>0 then
begin
Inc(Cnt);
S:=S+Format('%4d',[N]);
if (Cnt mod 10)=0 then S:=S+CRLF;
end;
end;
Memo.Lines.Add(S);
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
109 119 129 139 149 159 169 179 189 199
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919
 
Elapsed Time: 1.433 ms.
 
</pre>
 
 
=={{header|Draco}}==
Line 1,066 ⟶ 1,133:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_the_digits_of_n_is_substring_of_n}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Sum of the digits of n is substring of n 01.png]]
In '''[https://formulae.org/?example=Sum_of_the_digits_of_n_is_substring_of_n this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Sum of the digits of n is substring of n 02.png]]
 
=={{header|Go}}==
Line 1,698 ⟶ 1,767:
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ over findseq swap found ] is hasseq ( [ [ --> b )
 
[ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ digits
0 over witheach +
digits hasseq ] is subsum ( n --> b )
 
[] 1000 times
[ i^ subsum if
[ i^ join ] ]
dup echo
cr cr
say "There are " size echo say " numbers."</syntaxhighlight>
 
{{out}}
 
<pre>[ 0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900 910 911 912 913 914 915 916 917 918 919 ]
 
There are 48 numbers.</pre>
 
=={{header|Raku}}==
Line 1,708 ⟶ 1,804:
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Table (8 5) <Filter DigitSumSubstring <Iota 0 999>>>;
};
 
Cell {
s.W s.N, <Repeat s.W ' '> <Symb s.N>: e.C,
<Last s.W e.C>: (e.X) e.CI = e.CI;
}
 
Table {
(s.Cols s.CW) e.X = <Table () (s.Cols s.Cols s.CW) e.X>;
(e.Row) (s.Cols s.N s.CW), e.Row: {
= ;
e.Row = <Prout e.Row>;
};
(e.Row) (s.Cols 0 s.CW) e.X =
<Prout e.Row>
<Table () (s.Cols s.Cols s.CW) e.X>;
(e.Row) (s.Cols s.N s.CW) s.I e.X =
<Table (e.Row <Cell s.CW s.I>) (s.Cols <- s.N 1> s.CW) e.X>;
};
 
Repeat {
0 s.C = ;
s.N s.C = s.C <Repeat <- s.N 1> s.C>;
};
 
Filter {
s.F = ;
s.F s.I e.X, <Mu s.F s.I>: {
True = s.I <Filter s.F e.X>;
False = <Filter s.F e.X>;
};
};
 
Iota {
s.End s.End = s.End;
s.Start s.End = s.Start <Iota <+ 1 s.Start> s.End>;
};
 
DigitSumSubstring {
s.N, <Symb <DigitSum s.N>>: e.2,
<Symb s.N>: e.1 e.2 e.3 = True;
s.N = False;
};
 
DigitSum {
0 = 0;
s.N, <Divmod s.N 10>: (s.R) s.D = <+ s.D <DigitSum s.R>>;
};</syntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7
8 9 10 20 30 40 50 60
70 80 90 100 109 119 129 139
149 159 169 179 189 199 200 300
400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919</pre>
 
=={{header|REXX}}==
Line 1,793 ⟶ 1,948:
Found 48 numbers
done...
</pre>
 
=={{header|RPL}}==
≪ →STR 0 1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB STR→ + '''NEXT'''
→STR POS
≫ ‘'''∑DIN?'''’ STO
 
≪ { } 1 1000 '''FOR''' j '''IF''' j '''∑DIN? THEN''' j + '''END NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900 910 911 912 913 914 915 916 917 918 919 1000 }
</pre>
 
Line 1,800 ⟶ 1,967:
{{out}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 109, 119, 129, 139, 149, 159, 169, 179, 189, 199, 200, 300, 400, 500, 600, 700, 800, 900, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 1000]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="Rust">fn sum_digits( mut num : u32 ) -> u32 {
let mut sum : u32 = 0 ;
while num != 0 {
sum += num % 10 ;
num /= 10 ;
}
sum
}
 
fn main() {
let solution : Vec<u32> = (0..1000).filter( | &d | {
let digit_sum : u32 = sum_digits( d ) ;
let sumstring = digit_sum.to_string( ) ;
let sumstr : &str = sumstring.as_str( ) ;
let numstring : String = d.to_string( ) ;
let numstr : &str = numstring.as_str( ) ;
numstr.contains( &sumstr )
}).collect( ) ;
println!("{:?}" , solution ) ;
}</syntaxhighlight>
{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 109, 119, 129, 139, 149, 159, 169, 179, 189, 199, 200, 300, 400, 500, 600, 700, 800, 900, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919]
</pre>
 
Line 1,897 ⟶ 2,090:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var numbers = []
Line 1,910 ⟶ 2,101:
}
System.print("Numbers under 1,000 whose sum of digits is a substring of themselves:")
Fmt.tprint("$3d", numbers, 8)
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$3d", chunk)
System.print("\n%(numbers.count) such numbers found.")</syntaxhighlight>
 
2,093

edits