Largest five adjacent number: Difference between revisions
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12: | Line 12: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V largeNum = [random:(1..9)] [+] (0.<999).map(i -> random:(0..9)) |
||
V (maxNum, minNum) = (0, 99999) |
V (maxNum, minNum) = (0, 99999) |
||
Line 24: | Line 24: | ||
print(‘Largest 5-digit number extracted from random 1000-digit number: ’maxNum) |
print(‘Largest 5-digit number extracted from random 1000-digit number: ’maxNum) |
||
print(‘Smallest 5-digit number extracted from random 1000-digit number: #05’.format(minNum))</ |
print(‘Smallest 5-digit number extracted from random 1000-digit number: #05’.format(minNum))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 33: | Line 33: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
with Ada.Numerics.Discrete_Random; |
with Ada.Numerics.Discrete_Random; |
||
Line 62: | Line 62: | ||
Ada.Text_Io.Put_Line ("The largest number : " & Natural'Image (Large)); |
Ada.Text_Io.Put_Line ("The largest number : " & Natural'Image (Large)); |
||
Ada.Text_Io.Put_Line ("The smallest number: " & Natural'Image (Small)); |
Ada.Text_Io.Put_Line ("The smallest number: " & Natural'Image (Small)); |
||
end Adjacent_Numbers;</ |
end Adjacent_Numbers;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 71: | Line 71: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Adding the minimum number for good measure... |
Adding the minimum number for good measure... |
||
< |
<syntaxhighlight lang="algol68">BEGIN # generate 1000 random digits and find the largest/smallest numbers formed from 5 consecutive digits # |
||
[ 1 : 1000 ]CHAR digits; |
[ 1 : 1000 ]CHAR digits; |
||
FOR i TO UPB digits DO digits[ i ] := REPR ( ENTIER ( next random * 10 ) + ABS "0" ) OD; |
FOR i TO UPB digits DO digits[ i ] := REPR ( ENTIER ( next random * 10 ) + ABS "0" ) OD; |
||
Line 91: | Line 91: | ||
print( ( "Largest 5 consecutive digits from 1000 random digits: ", max number, newline ) ); |
print( ( "Largest 5 consecutive digits from 1000 random digits: ", max number, newline ) ); |
||
print( ( "Smallest 5 consecutive digits from 1000 random digits: ", min number, newline ) ) |
print( ( "Smallest 5 consecutive digits from 1000 random digits: ", min number, newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 100: | Line 100: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">⌈/((⊣+10×⊢)/(⌽↓))⌺5⊢(-⎕IO)+?1000/10</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
(example) |
(example) |
||
Line 106: | Line 106: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">maxNum := 0, str := "" |
||
loop, 1000 |
loop, 1000 |
||
{ |
{ |
||
Line 119: | Line 119: | ||
} |
} |
||
MsgBox % result := output "`n`nLargest five adjacent digits = " maxNum |
MsgBox % result := output "`n`nLargest five adjacent digits = " maxNum |
||
. "`n`nSmallest five adjacent digits = " minNum</ |
. "`n`nSmallest five adjacent digits = " minNum</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3893212622395522104846091986776081862634026945871752892124324578621089065097043281907406149009719673003318226562809101957181871693776164191416491334 |
<pre>3893212622395522104846091986776081862634026945871752892124324578621089065097043281907406149009719673003318226562809101957181871693776164191416491334 |
||
Line 134: | Line 134: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f LARGEST_FIVE_ADJACENT_NUMBER.AWK |
# syntax: GAWK -f LARGEST_FIVE_ADJACENT_NUMBER.AWK |
||
BEGIN { |
BEGIN { |
||
Line 163: | Line 163: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 172: | Line 172: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang="bqn">⌈´(⊣+10×⊢)˝⌽⍉5↕1000 •rand.Range 10</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
(example) |
(example) |
||
Line 178: | Line 178: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
Line 212: | Line 212: | ||
printf("%d\n", largest); |
printf("%d\n", largest); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
(example) |
(example) |
||
<pre>99931</pre> |
<pre>99931</pre> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% Generate a number of N random digits |
||
random_digits = proc (n: int) returns (sequence[int]) |
random_digits = proc (n: int) returns (sequence[int]) |
||
digits: array[int] := array[int]$predict(1,n) |
digits: array[int] := array[int]$predict(1,n) |
||
Line 256: | Line 256: | ||
stream$putl(po, "Smallest: " || int$unparse(min)) |
stream$putl(po, "Smallest: " || int$unparse(min)) |
||
stream$putl(po, "Largest: " || int$unparse(max)) |
stream$putl(po, "Largest: " || int$unparse(max)) |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Smallest: 144 |
<pre>Smallest: 144 |
||
Line 262: | Line 262: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Largest five adjacent number. Nigel Galloway: September 28th., 2021 |
// Largest five adjacent number. Nigel Galloway: September 28th., 2021 |
||
let n()=let n()=System.Random().Next(10) in Seq.unfold(fun g->Some(g,(g%10000)*10+n()))(n()*10000+n()*1000+n()*100+n()*10+n()) |
let n()=let n()=System.Random().Next(10) in Seq.unfold(fun g->Some(g,(g%10000)*10+n()))(n()*10000+n()*1000+n()*100+n()*10+n()) |
||
printfn $"Largest 5 adjacent digits are %d{(n()|>Seq.take 995|>Seq.max)}" |
printfn $"Largest 5 adjacent digits are %d{(n()|>Seq.take 995|>Seq.max)}" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 274: | Line 274: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: grouping io math.functions prettyprint random sequences ; |
||
1000 10^ random unparse 5 <clumps> supremum print</ |
1000 10^ random unparse 5 <clumps> supremum print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 284: | Line 284: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Generate the number digit by digit, and test as we go. If the task didn't specifically ask to generate the whole 1,000 digit number I wouldn't bother storing more than five of its digits at a time. |
Generate the number digit by digit, and test as we go. If the task didn't specifically ask to generate the whole 1,000 digit number I wouldn't bother storing more than five of its digits at a time. |
||
< |
<syntaxhighlight lang="freebasic"> |
||
randomize timer |
randomize timer |
||
dim as ubyte number(0 to 999) |
dim as ubyte number(0 to 999) |
||
Line 298: | Line 298: | ||
next i |
next i |
||
print highest, lowest |
print highest, lowest |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 307: | Line 307: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 340: | Line 340: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 350: | Line 350: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">>./5([+10*])/@|:\?1000#10</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
(example) |
(example) |
||
Line 356: | Line 356: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">dig = rand(0:9, 1000) |
||
@show maximum(evalpoly(10, dig[i:i+4]) for i in 1:length(dig)-4) |
@show maximum(evalpoly(10, dig[i:i+4]) for i in 1:length(dig)-4) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
maximum((evalpoly(10, dig[i:i + 4]) for i = 1:length(dig) - 4)) = 99993 |
maximum((evalpoly(10, dig[i:i + 4]) for i = 1:length(dig) - 4)) = 99993 |
||
Line 377: | Line 377: | ||
</pre> |
</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">MinMax[FromDigits /@ Partition[RandomInteger[{0, 9}, 1000], 5, 1]]</syntaxhighlight> |
||
{{out}}<pre>{104,99984}</pre> |
{{out}}<pre>{104,99984}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import random, strutils |
||
randomize() |
randomize() |
||
Line 408: | Line 408: | ||
if n > max: max = n |
if n > max: max = n |
||
echo "Largest 5-digit number extracted from random 1000-digit number: ", max.join()</ |
echo "Largest 5-digit number extracted from random 1000-digit number: ", max.join()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 415: | Line 415: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} inspired by [[Largest_five_adjacent_number#Wren|Wren]]<BR>Assumes that there at least is a "1" 4 digits before end of all digits.Else I have to include sysutils and s := Format('%.5d',[i]); for leading zeros. |
{{works with|Free Pascal}} inspired by [[Largest_five_adjacent_number#Wren|Wren]]<BR>Assumes that there at least is a "1" 4 digits before end of all digits.Else I have to include sysutils and s := Format('%.5d',[i]); for leading zeros. |
||
< |
<syntaxhighlight lang="pascal"> |
||
var |
var |
||
digits, |
digits, |
||
Line 432: | Line 432: | ||
end; |
end; |
||
writeln(s, ' found as largest 5 digit number ') |
writeln(s, ' found as largest 5 digit number ') |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>99889 found as largest 5 digit number </pre> |
<pre>99889 found as largest 5 digit number </pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Largest_five_adjacent_number |
use strict; # https://rosettacode.org/wiki/Largest_five_adjacent_number |
||
Line 445: | Line 445: | ||
my @n; |
my @n; |
||
@n[ /(?=(\d{5}))/g ] = (); |
@n[ /(?=(\d{5}))/g ] = (); |
||
print "$#n\n";</ |
print "$#n\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 452: | Line 452: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">shlong</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">shlong</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
||
Line 476: | Line 476: | ||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$]</span> |
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$]</span> |
||
<span style="color: #000000;">shlong</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">shlong</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 485: | Line 485: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
Seeding the random number generator directly with the datetime stamp produces a warning that it will be deprecated in Python 3.9, hence the "hack" of creating a string out of the timestamp and then seeding with it. |
Seeding the random number generator directly with the datetime stamp produces a warning that it will be deprecated in Python 3.9, hence the "hack" of creating a string out of the timestamp and then seeding with it. |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
#Aamrun, 5th October 2021 |
#Aamrun, 5th October 2021 |
||
Line 509: | Line 509: | ||
print("Largest 5-adjacent number found ", maxNum) |
print("Largest 5-adjacent number found ", maxNum) |
||
print("Smallest 5-adjacent number found ", minNum) |
print("Smallest 5-adjacent number found ", minNum) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results from multiple runs : |
Results from multiple runs : |
||
{{out}} |
{{out}} |
||
Line 545: | Line 545: | ||
Do it 5 times for variety, it's random after all. |
Do it 5 times for variety, it's random after all. |
||
<lang |
<syntaxhighlight lang="raku" line>(^௰).roll(௲).rotor(5 => -4)».join.minmax.bounds.put xx 5</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
<pre>00371 99975 |
<pre>00371 99975 |
||
Line 555: | Line 555: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
digit = "" |
digit = "" |
||
max = 0 |
max = 0 |
||
Line 582: | Line 582: | ||
see "The smallest number is:" + nl |
see "The smallest number is:" + nl |
||
see min + nl |
see min + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 592: | Line 592: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">digits = %w(0 1 2 3 4 5 6 7 8 9) |
||
arr = Array.new(1000){ digits.sample } |
arr = Array.new(1000){ digits.sample } |
||
puts "minimum sequence %s, maximum sequence %s." % arr.each_cons(5).minmax_by{|slice| slice.join.to_i}.map(&:join) |
puts "minimum sequence %s, maximum sequence %s." % arr.each_cons(5).minmax_by{|slice| slice.join.to_i}.map(&:join) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>minimum sequence 00096, maximum sequence 99508. |
<pre>minimum sequence 00096, maximum sequence 99508. |
||
Line 601: | Line 601: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var k = 5 |
||
var n = 1e1000.irand |
var n = 1e1000.irand |
||
Line 609: | Line 609: | ||
say ("Min #{k}-digit sub-number: ", c.min_by { .digits2num }.flip.join) |
say ("Min #{k}-digit sub-number: ", c.min_by { .digits2num }.flip.join) |
||
say ("Max #{k}-digit sub-number: ", c.max_by { .digits2num }.flip.join)</ |
say ("Max #{k}-digit sub-number: ", c.max_by { .digits2num }.flip.join)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 619: | Line 619: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">import rand |
||
import rand.seed |
import rand.seed |
||
import strings |
import strings |
||
Line 645: | Line 645: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The largest number formed from 5 adjacent digits (99928) is: 99,928 |
<pre>The largest number formed from 5 adjacent digits (99928) is: 99,928 |
||
Line 653: | Line 653: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
Very simple approach as there's little need for speed here. |
Very simple approach as there's little need for speed here. |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 669: | Line 669: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 679: | Line 679: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">char Number(1000); |
||
int Num, Max, I, J; |
int Num, Max, I, J; |
||
[for I:= 0 to 1000-1 do \generate 1000-digit number |
[for I:= 0 to 1000-1 do \generate 1000-digit number |
||
Line 692: | Line 692: | ||
]; |
]; |
||
IntOut(0, Max); |
IntOut(0, Max); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 17:29, 27 August 2022
- Task
Generate random 1000-digit number.
Find the five adjacent digits in the 1000-digit number that form the largest 5-digit number.
- Extra credit
Find the five adjacent digits in the 1000-digit number that form the smallest 5-digit number.
11l
V largeNum = [random:(1..9)] [+] (0.<999).map(i -> random:(0..9))
V (maxNum, minNum) = (0, 99999)
L(i) 996
V num = Int(largeNum[i.+5].join(‘’))
I num > maxNum
maxNum = num
E I num < minNum
minNum = num
print(‘Largest 5-digit number extracted from random 1000-digit number: ’maxNum)
print(‘Smallest 5-digit number extracted from random 1000-digit number: #05’.format(minNum))
- Output:
Largest 5-digit number extracted from random 1000-digit number: 99902 Smallest 5-digit number extracted from random 1000-digit number: 00043
Ada
with Ada.Text_Io;
with Ada.Numerics.Discrete_Random;
procedure Adjacent_Numbers is
Adjacent_Length : constant := 5;
subtype Digit is Character range '0' .. '9';
package Random_Digits
is new Ada.Numerics.Discrete_Random (Digit);
Gen : Random_Digits.Generator;
Line : String (1 .. 1000);
Large : Natural := Natural'First;
Small : Natural := Natural'Last;
begin
Random_Digits.Reset (Gen);
Line := (others => Random_Digits.Random (Gen));
for I in Line'First .. Line'Last - Adjacent_Length + 1 loop
declare
Window : String renames Line (I .. I + Adjacent_Length - 1);
begin
Large := Natural'Max (Large, Natural'Value (Window));
Small := Natural'Min (Small, Natural'Value (Window));
end;
end loop;
Ada.Text_Io.Put_Line ("The largest number : " & Natural'Image (Large));
Ada.Text_Io.Put_Line ("The smallest number: " & Natural'Image (Small));
end Adjacent_Numbers;
- Output:
The largest number : 99625 The smallest number: 102
ALGOL 68
Adding the minimum number for good measure...
BEGIN # generate 1000 random digits and find the largest/smallest numbers formed from 5 consecutive digits #
[ 1 : 1000 ]CHAR digits;
FOR i TO UPB digits DO digits[ i ] := REPR ( ENTIER ( next random * 10 ) + ABS "0" ) OD;
STRING max number := digits[ 1 : 5 ];
STRING min number := digits[ 1 : 5 ];
FOR i FROM 2 TO UPB digits - 4 DO
STRING next number = digits[ i : i + 4 ];
IF next number > max number
THEN
# found a new higher number #
max number := next number
FI;
IF next number < min number
THEN
# found a new lower number #
min number := next number
FI
OD;
print( ( "Largest 5 consecutive digits from 1000 random digits: ", max number, newline ) );
print( ( "Smallest 5 consecutive digits from 1000 random digits: ", min number, newline ) )
END
- Output:
Largest 5 consecutive digits from 1000 random digits: 99987 Smallest 5 consecutive digits from 1000 random digits: 00119
APL
⌈/((⊣+10×⊢)/(⌽↓))⌺5⊢(-⎕IO)+?1000/10
- Output:
(example)
99994
AutoHotkey
maxNum := 0, str := ""
loop, 1000
{
Random, rnd, 0, 9
str .= rnd
output .= rnd . (Mod(A_Index, 148) ? "" : "`n")
if A_Index < 5
continue
num := SubStr(str, A_Index-4, 5)
maxNum := maxNum > num ? maxNum : num
minNum := A_Index = 5 ? num : minNum < num ? minNum : num
}
MsgBox % result := output "`n`nLargest five adjacent digits = " maxNum
. "`n`nSmallest five adjacent digits = " minNum
- Output:
3893212622395522104846091986776081862634026945871752892124324578621089065097043281907406149009719673003318226562809101957181871693776164191416491334 2509291361707848297387923254298547833186351133036771338719578505791529263806019240009497155124458943732581184022226943392528107498748575424217651885 3083736872582691290721469942482918430078673685947447234032602113276631102983248999047362916320523840282929255314468323644427797630259187509914424396 1523615571637081320270791095221484894567420630155741441396012393172769867922862248399483054652921274863786220527596050784952102267710198517665662903 6335615800351254988779849447078262460051794249274045128158246939351902901862546960248213286880570086476859341012102414828750098051948784732121573660 9618754338433412518619240496583375235634416473003920360759949694724646721954909867058588446320222792637823988375313876167705092153587245148819122980 2777308429997046827297505483667631338885207838402941712216614732232703459440770039141898763110002290662921501156 Largest five adjacent digits = 99970 Smallest five adjacent digits = 00022
AWK
# syntax: GAWK -f LARGEST_FIVE_ADJACENT_NUMBER.AWK
BEGIN {
limit = 1000
width = 5
max_n = 0
for (i=1; i<=width; i++) {
min_n = min_n "9"
}
srand()
for (i=1; i<=limit; i++) {
digits = digits int(rand() * 10)
}
for (i=1; i<=limit-width+1; i++) {
n = substr(digits,i,width)
if (n > max_n) {
max_n = n
max_pos = i
}
if (n < min_n) {
min_n = n
min_pos = i
}
}
printf("look for %d digit number using %d digits\n",width,limit)
printf("largest %0*d in positions %d-%d\n",width,max_n,max_pos,max_pos+width-1)
printf("smallest %0*d in positions %d-%d\n",width,min_n,min_pos,min_pos+width-1)
exit(0)
}
- Output:
look for 5 digit number using 1000 digits largest 99873 in positions 300-304 smallest 00099 in positions 697-701
BQN
⌈´(⊣+10×⊢)˝⌽⍉5↕1000 •rand.Range 10
- Output:
(example)
99991
C
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#define DIGITS 1000
#define NUMSIZE 5
uint8_t randomDigit() {
uint8_t d;
do {d = rand() & 0xF;} while (d >= 10);
return d;
}
int numberAt(uint8_t *d, int size) {
int acc = 0;
while (size--) acc = 10*acc + *d++;
return acc;
}
int main() {
uint8_t digits[DIGITS];
int i, largest = 0;
srand(time(NULL));
for (i=0; i<DIGITS; i++) digits[i] = randomDigit();
for (i=0; i<DIGITS-NUMSIZE; i++) {
int here = numberAt(&digits[i], NUMSIZE);
if (here > largest) largest = here;
}
printf("%d\n", largest);
return 0;
}
- Output:
(example)
99931
CLU
% Generate a number of N random digits
random_digits = proc (n: int) returns (sequence[int])
digits: array[int] := array[int]$predict(1,n)
% A number never starts with a zero
array[int]$addh(digits, 1+random$next(9))
for i: int in int$from_to(1,n-1) do
array[int]$addh(digits, random$next(10))
end
return(sequence[int]$a2s(digits))
end random_digits
% Find the largest and smallest N-adjacent number in the digits
find_min_max = proc (n: int, digits: sequence[int]) returns (int,int)
min: int := 10**n % Guaranteed to be bigger than any N-adjacent number
max: int := 0
for i: int in int$from_to(1, sequence[int]$size(digits)-n) do
cur: int := 0
for j: int in int$from_to(0, n-1) do
cur := 10*cur + digits[i+j]
end
if cur<min then min:=cur end
if cur>max then max:=cur end
end
return(min, max)
end find_min_max
start_up = proc ()
% Seed the RNG with the current time
d: date := now()
random$seed(d.second + 60*(d.minute + 60*d.hour))
% Find the minimum and maximum 5-adjacent numbers in a 1000-digit number
min, max: int := find_min_max(5, random_digits(1000))
po: stream := stream$primary_output()
stream$putl(po, "Smallest: " || int$unparse(min))
stream$putl(po, "Largest: " || int$unparse(max))
end start_up
- Output:
Smallest: 144 Largest: 99951
F#
// Largest five adjacent number. Nigel Galloway: September 28th., 2021
let n()=let n()=System.Random().Next(10) in Seq.unfold(fun g->Some(g,(g%10000)*10+n()))(n()*10000+n()*1000+n()*100+n()*10+n())
printfn $"Largest 5 adjacent digits are %d{(n()|>Seq.take 995|>Seq.max)}"
- Output:
Largest 5 adjacent digits are 99914
Factor
USING: grouping io math.functions prettyprint random sequences ;
1000 10^ random unparse 5 <clumps> supremum print
- Output:
99987
FreeBASIC
Generate the number digit by digit, and test as we go. If the task didn't specifically ask to generate the whole 1,000 digit number I wouldn't bother storing more than five of its digits at a time.
randomize timer
dim as ubyte number(0 to 999)
dim as uinteger seg, highest = 0, lowest = 100000
for i as uinteger = 0 to 999
number(i) = int(rnd*10)
if i >= 4 then
seg = number(i) + 10*number(i-1) + 100*number(i-2) +_
1000*number(i-3) + 10000*number(i-4)
if seg < lowest then lowest = seg
if seg > highest then highest = seg
end if
next i
print highest, lowest
- Output:
99748 31
Go
package main
import (
"fmt"
"math/rand"
"rcu"
"strings"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
var sb strings.Builder
for i := 0; i < 1000; i++ {
sb.WriteByte(byte(rand.Intn(10) + 48))
}
number := sb.String()
for i := 99999; i >= 0; i-- {
quintet := fmt.Sprintf("%05d", i)
if strings.Contains(number, quintet) {
ci := rcu.Commatize(i)
fmt.Printf("The largest number formed from 5 adjacent digits (%s) is: %6s\n", quintet, ci)
break
}
}
for i := 0; i <= 99999; i++ {
quintet := fmt.Sprintf("%05d", i)
if strings.Contains(number, quintet) {
ci := rcu.Commatize(i)
fmt.Printf("The smallest number formed from 5 adjacent digits (%s) is: %6s\n", quintet, ci)
return
}
}
}
- Output:
Sample run:
The largest number formed from 5 adjacent digits (99928) is: 99,928 The smallest number formed from 5 adjacent digits (00120) is: 120
J
>./5([+10*])/@|:\?1000#10
- Output:
(example)
99929
Julia
dig = rand(0:9, 1000)
@show maximum(evalpoly(10, dig[i:i+4]) for i in 1:length(dig)-4)
- Output:
maximum((evalpoly(10, dig[i:i + 4]) for i = 1:length(dig) - 4)) = 99993
Or, using strings, and see Nigel's comment in the discussion:
julia> setprecision(3324) 3324 julia> s = string(BigFloat(pi))[3:end] "141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365" ⋯ 180 bytes ⋯ "66940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019898" julia> m, pos = maximum((s[i:i+4], i) for i in 1:length(s)-4) ("99999", 763) julia> println("Maximum is $m at position $pos.") Maximum is 99999 at position 763.
Mathematica / Wolfram Language
MinMax[FromDigits /@ Partition[RandomInteger[{0, 9}, 1000], 5, 1]]
- Output:
{104,99984}
Nim
import random, strutils
randomize()
const N = 1000
type Digit = 0..9
# Build a 1000-digit number.
var number: array[1..N, Digit]
number[1] = rand(1..9) # Make sure the first digit is not 0.
for i in 1..N: number[i] = rand(9)
func `>`(s1, s2: seq[Digit]): bool =
## Compare two digit sequences.
## Defining `<` rather than `>` would work too.
assert s1.len == s2.len
for i in 0..s1.high:
let comp = cmp(s1[i], s2[i])
if comp != 0: return comp == 1
result = false
var max = @[Digit 0, 0, 0, 0, 0]
for i in 5..N:
let n = number[i-4..i]
if n > max: max = n
echo "Largest 5-digit number extracted from random 1000-digit number: ", max.join()
- Output:
Largest 5-digit number extracted from random 1000-digit number: 99855
Pascal
inspired by Wren
Assumes that there at least is a "1" 4 digits before end of all digits.Else I have to include sysutils and s := Format('%.5d',[i]); for leading zeros.
var
digits,
s : AnsiString;
i : LongInt;
begin
randomize;
setlength(digits,1000);
for i := 1 to 1000 do
digits[i] := chr(random(10)+ord('0'));
for i := 99999 downto 0 do
begin
str(i:5,s);
if Pos(s,digits) > 0 then
break;
end;
writeln(s, ' found as largest 5 digit number ')
end.
- Output:
99889 found as largest 5 digit number
Perl
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Largest_five_adjacent_number
use warnings;
$_ = join '', map int rand 10, 1 .. 1e3;
my @n;
@n[ /(?=(\d{5}))/g ] = ();
print "$#n\n";
- Output:
99899
Phix
with javascript_semantics procedure shlong(string s) string hi5 = s[1..5], lo5 = hi5 for i=2 to length(s)-4 do string s5 = s[i..i+4] hi5 = max(hi5,s5) lo5 = min(lo5,s5) end for printf(1,"String %s: hi5:%s, lo5:%s\n",{shorten(s),hi5,lo5}) end procedure string s = repeat(' ',1000) for i=1 to length(s) do s[i] = rand(10)-1+'0' end for shlong(s) include mpfr.e mpfr pi = mpfr_init(0,-1001) -- (set precision to 1,000 dp, plus the "3.") mpfr_const_pi(pi) s = mpfr_get_fixed(pi,1000) s = s[3..$] shlong(s)
- Output:
String 35369847249221789712...55814915156742014134 (1,000 digits): hi5:99969, lo5:00013 String 14159265358979323846...66111959092164201989 (1,000 digits): hi5:99999, lo5:00031
Python
Seeding the random number generator directly with the datetime stamp produces a warning that it will be deprecated in Python 3.9, hence the "hack" of creating a string out of the timestamp and then seeding with it.
#Aamrun, 5th October 2021
from random import seed,randint
from datetime import datetime
seed(str(datetime.now()))
largeNum = [randint(1,9)]
for i in range(1,1000):
largeNum.append(randint(0,9))
maxNum,minNum = 0,99999
for i in range(0,994):
num = int("".join(map(str,largeNum[i:i+5])))
if num > maxNum:
maxNum = num
elif num < minNum:
minNum = num
print("Largest 5-adjacent number found ", maxNum)
print("Smallest 5-adjacent number found ", minNum)
Results from multiple runs :
- Output:
Largest 5-adjacent number found 99743 Smallest 5-adjacent number found 102 Largest 5-adjacent number found 99965 Smallest 5-adjacent number found 84 Largest 5-adjacent number found 99808 Smallest 5-adjacent number found 58 Largest 5-adjacent number found 99938 Smallest 5-adjacent number found 10 Largest 5-adjacent number found 99957 Smallest 5-adjacent number found 35
Raku
Show minimum too because... why not?
Use some Tamil Unicode numbers for brevity, and for amusement purposes.
௰ - Tamil number ten ௲ - Tamil number one thousand
Do it 5 times for variety, it's random after all.
(^௰).roll(௲).rotor(5 => -4)».join.minmax.bounds.put xx 5
- Sample output:
00371 99975 00012 99982 00008 99995 00012 99945 00127 99972
Ring
digit = ""
max = 0
min = 99999
limit = 1000
for n = 1 to limit
rand = random(9)
randStr = string(rand)
digit += randStr
next
for n = 1 to len(digit)-5
res = substr(digit,n,5)
resNum = number(res)
if resNum > max
max = resNum
ok
if resNum < min
min = res
ok
next
see "The largest number is:" + nl
see max + nl
see "The smallest number is:" + nl
see min + nl
- Output:
The largest number is: 99638 The smallest number is: 00118
Ruby
digits = %w(0 1 2 3 4 5 6 7 8 9)
arr = Array.new(1000){ digits.sample }
puts "minimum sequence %s, maximum sequence %s." % arr.each_cons(5).minmax_by{|slice| slice.join.to_i}.map(&:join)
- Output:
minimum sequence 00096, maximum sequence 99508.
Sidef
var k = 5
var n = 1e1000.irand
say "length(n) = #{n.len}"
var c = n.digits.cons(k)
say ("Min #{k}-digit sub-number: ", c.min_by { .digits2num }.flip.join)
say ("Max #{k}-digit sub-number: ", c.max_by { .digits2num }.flip.join)
- Output:
length(n) = 1000 Min 5-digit sub-number: 00072 Max 5-digit sub-number: 99861
Vlang
import rand
import rand.seed
import strings
fn main() {
rand.seed(seed.time_seed_array(2))
mut sb := strings.new_builder(128)
for _ in 0..1000 {
sb.write_byte(u8(rand.intn(10) or {0} + 48))
}
number := sb.str()
println('>> $number')
for i := 99999; i >= 0; i-- {
quintet := "${i:05}"
if number.contains(quintet) {
println("The largest number formed from 5 adjacent digits ($quintet) is: ${i:6}")
break
}
}
for i := 0; i <= 99999; i++ {
quintet := "${i:05}"
if number.contains(quintet) {
println("The smallest number formed from 5 adjacent digits ($quintet) is: ${i:6}")
return
}
}
}
- Output:
The largest number formed from 5 adjacent digits (99928) is: 99,928 The smallest number formed from 5 adjacent digits (00120) is: 120
Wren
Very simple approach as there's little need for speed here.
import "random" for Random
import "/fmt" for Fmt
var rand = Random.new()
var digits = List.filled(1000, 0)
for (i in 0...999) digits[i] = rand.int(10)
var number = digits.join()
for (r in [99999..0, 0..99999]) {
var target = (r.from == 0) ? "smallest" : "largest "
for (i in r) {
var quintet = Fmt.swrite("$05d", i)
if (number.contains(quintet)) {
Fmt.print("The $s number formed from 5 adjacent digits ($s) is: $,6d", target, quintet, i)
break
}
}
}
- Output:
Sample output:
The largest number formed from 5 adjacent digits (99830) is: 99,830 The smallest number formed from 5 adjacent digits (00154) is: 154
XPL0
char Number(1000);
int Num, Max, I, J;
[for I:= 0 to 1000-1 do \generate 1000-digit number
Number(I):= Ran(10);
Max:= 0; \find its largest 5-digit number
for I:= 0 to 1000-5 do
[Num:= 0;
for J:= 0 to 5-1 do
Num:= Num*10 + Number(I+J);
if Num > Max then
Max:= Num;
];
IntOut(0, Max);
]
- Output:
99930