Largest five adjacent number: Difference between revisions

Added Easylang
m (→‎{{header|Pascal}}: more comment)
(Added Easylang)
 
(40 intermediate revisions by 26 users not shown)
Line 5:
<br> Find the '''five''' adjacent digits in the 1000-digit number that form the largest 5-digit number.
<br>
;Extra credit
Find the '''five''' adjacent digits in the 1000-digit number that form the smallest 5-digit number.
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="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))</syntaxhighlight>
 
{{out}}
<pre>
Largest 5-digit number extracted from random 1000-digit number: 99902
Smallest 5-digit number extracted from random 1000-digit number: 00043
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="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;</syntaxhighlight>
{{out}}
<pre>
The largest number : 99625
The smallest number: 102
</pre>
 
=={{header|ALGOL 68}}==
Adding the minimum number for good measure...
<langsyntaxhighlight lang="algol68">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;
Line 30 ⟶ 91:
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</langsyntaxhighlight>
{{out}}
<pre>
Largest 5 consecutive digits from 1000 random digits: 99987
Smallest 5 consecutive digits from 1000 random digits: 00119
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">⌈/((⊣+10×⊢)/(⌽↓))⌺5⊢(-⎕IO)+?1000/10</syntaxhighlight>
{{out}}
(example)
<pre>99994</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">N: join to [:string] map 1..100 'x -> random 1000000000 9999999999
 
i: 0
maxNum: 0
minNum: ∞
while [i < (size N)-5][
num: to :integer join @[N\[i] N\[i+1] N\[i+2] N\[i+3] N\[i+4]]
if num > maxNum -> maxNum: num
if num < minNum -> minNum: num
i: i + 1
]
 
print "Our random 1000-digit number is:"
print N
 
print ""
 
print ["Max 5-adjacent number found:" maxNum]
print ["Min 5-adjacent number found:" (repeat "0" 5-(size to :string minNum)) ++ (to :string minNum)]</syntaxhighlight>
 
{{out}}
 
<pre>Our random 1000-digit number is:
2540956677308157418624519953263471599696918276171651168484519407031160813613006352660058588944602704848634276542837184618726044674117357036813240557325769932073351534364289297094415941273117151277729576200542643185699525405079189015204192029912043004161916366921458912887890652627268028071729897387395041640352395354106991129061548748712499227024213135531365974620993813773921850969630855401781344832397898392812417729744785629765286216304456806870691502938136795922685099816652448188701308354551593078486609811394420601431484916913833634669083737749230355341380266781803894385432741405633278873213701238310761908151961510643290964548205746238459266137202173265468217401777681775761126374654289733873900330799576500024068191362342162163615972164105625935627483920193464168192083262176697432155066174175594837721476581087940310642712981291006889657297350894628612724944063786324456854104801432247483498384207351647946918119868105898645178074174003550762101547842674605061792172905254724197215648686667
 
Max 5-adjacent number found: 99816
Min 5-adjacent number found: 00024</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
<pre>3893212622395522104846091986776081862634026945871752892124324578621089065097043281907406149009719673003318226562809101957181871693776164191416491334
2509291361707848297387923254298547833186351133036771338719578505791529263806019240009497155124458943732581184022226943392528107498748575424217651885
3083736872582691290721469942482918430078673685947447234032602113276631102983248999047362916320523840282929255314468323644427797630259187509914424396
1523615571637081320270791095221484894567420630155741441396012393172769867922862248399483054652921274863786220527596050784952102267710198517665662903
6335615800351254988779849447078262460051794249274045128158246939351902901862546960248213286880570086476859341012102414828750098051948784732121573660
9618754338433412518619240496583375235634416473003920360759949694724646721954909867058588446320222792637823988375313876167705092153587245148819122980
2777308429997046827297505483667631338885207838402941712216614732232703459440770039141898763110002290662921501156
 
Largest five adjacent digits = 99970
 
Smallest five adjacent digits = 00022</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="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)
}
</syntaxhighlight>
{{out}}
<pre>
look for 5 digit number using 1000 digits
largest 99873 in positions 300-304
smallest 00099 in positions 697-701
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">dim number(1000)
highest = 0
lowest = 100000
for i = 0 to 999
number[i] = int(rand*10)
if i >= 4 then
tmp = number[i] + 10*number[i-1] + 100*number[i-2] + 1000*number[i-3] + 10000*number[i-4]
if tmp < lowest then lowest = tmp
if tmp > highest then highest = tmp
end if
next i
print highest, lowest</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">100 randomize timer
110 dim number(999)
120 highest = 0
130 lowest = 100000
140 for i = 0 to 999
150 number(i) = int(rnd(10))
160 if i >= 4 then
170 tmp = number(i)+10*number(i-1)+100*number(i-2)+1000*number(i-3)+10000*number(i-4)
180 if tmp < lowest then lowest = tmp
190 if tmp > highest then highest = tmp
200 endif
210 next i
220 print highest,lowest
230 end</syntaxhighlight>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public number[1000] As Byte
 
Public Sub Main()
Randomize
Dim tmp As Integer, highest As Integer = 0, lowest As Integer = 100000
For i As Integer = 0 To 999
number[i] = Int(Rnd(10))
If i >= 4 Then
tmp = number[i] + 10 * number[i - 1] + 100 * number[i - 2] + 1000 * number[i - 3] + 10000 * number[i - 4]
If tmp < lowest Then lowest = tmp
If tmp > highest Then highest = tmp
End If
Next
Print highest, lowest
End</syntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">OpenConsole()
Dim number.i(999)
highest.i = 0
lowest.i = 100000
For i.i = 0 To 999
number(i) = Random(10)
If i >= 4:
tmp = number(i) + 10*number(i-1) + 100*number(i-2) + 1000*number(i-3) + 10000*number(i-4)
If tmp < lowest: lowest = tmp: EndIf
If tmp > highest: highest = tmp: EndIf
EndIf
Next i
PrintN(Str(highest) + #TAB$ + Str(lowest))</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">RANDOMIZE TIMER
DIM number(0 TO 999)
highest = 0
lowest = 100000
FOR i = 0 TO 999
number(i) = INT(RND * 10)
IF i >= 4 THEN
tmp = number(i) + 10 * number(i - 1) + 100 * number(i - 2) + 1000 * number(i - 3) + 10000 * number(i - 4)
IF tmp < lowest THEN lowest = tmp
IF tmp > highest THEN highest = tmp
END IF
NEXT i
PRINT highest, lowest
END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">RANDOMIZE
DIM number(0 TO 999)
LET highest = 0
LET lowest = 100000
FOR i = 0 TO 999
LET number(i) = INT(RND*10)
IF i >= 4 THEN
LET tmp = number(i)+10*number(i-1)+100*number(i-2)+1000*number(i-3)+10000*number(i-4)
IF tmp < lowest THEN LET lowest = tmp
IF tmp > highest THEN LET highest = tmp
END IF
NEXT i
PRINT highest, lowest
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">dim number(999)
highest = 0
lowest = 100000
for i = 0 to 999
number(i) = int(ran(10))
if i >= 4 then
tmp = number(i) + 10*number(i-1) + 100*number(i-2) + 1000*number(i-3) + 10000*number(i-4)
if tmp < lowest lowest = tmp
if tmp > highest highest = tmp
fi
next i
print highest, lowest</syntaxhighlight>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">⌈´(⊣+10×⊢)˝⌽⍉5↕1000 •rand.Range 10</syntaxhighlight>
{{out}}
(example)
<pre>99991</pre>
 
=={{header|C}}==
<syntaxhighlight lang="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;
}</syntaxhighlight>
{{out}}
(example)
<pre>99931</pre>
=={{header|CLU}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
<pre>Smallest: 144
Largest: 99951</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function Get5DigitNumber(S: string; Off: integer): integer;
{Extract 5 digit number from string at Off}
var I: integer;
var NS: string;
begin
NS:=Copy(S,Off,5);
Result:=StrToIntDef(NS,-1);
end;
 
 
 
function BreakupString(S: string): string;
{Breakup thousand digit number for easy display}
var I: integer;
begin
for I:=1 to Length(S) do
begin
Result:=Result+S[I];
if (I mod 55)=0 then Result:=Result+#$0D#$0A;
end;
end;
 
procedure FiveDigitNumber(Memo: TMemo);
{Find the largest and small 5 digit sequence}
{in 1000 digit number}
var S: string;
var N,I: integer;
var Largest,Smallest: integer;
begin
Smallest:=High(Integer);
Largest:=0;
for I:=1 to 1000 do
S:=S+Char(Random(10)+$30);
for I:=1 to Length(S)-5 do
begin
N:=Get5DigitNumber(S,I);
if N>Largest then Largest:=N;
if N<Smallest then Smallest:=N;
end;
Memo.Lines.Add(BreakupString(S));
Memo.Lines.Add('Largest: '+IntToStr(Largest));;
Memo.Lines.Add('Smallest: '+IntToStr(Smallest));;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
0082263134040802937368731342824182794880115050767752659
6926207485596307977119758620628125911215421677000178364
7438810001625238336693427757455861441056098692774612931
9301856160395349334087194184285169534216966507128749101
0333045468523586265833674268791722749102838792380205401
7335212073765802860114410575280403628540910018912794058
9569778977033072890894634763659190635686944921467068416
0978402580498879216810854417805724457730620420683349740
8203884243646784563247619038458645194136841413688117232
0960606571886477139587251334596793042923055521495533796
5592094928040937883628134090110628164451939278452734493
5741344340195488542852682604882967292438604245256357719
4755578568409079269700382959730067457921191314413220282
3502307407547002586284406642530858066838890257743184196
5040611036453640792847940715686736822030381083124941163
3588177613294220880152655471721880286144478485085399563
1095924640071825166992021998152653370680394470682198029
3879102724160697653653330275506532525946257246355415772
4978409544
Largest: 99815
Smallest: 16
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// 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)}"
</syntaxhighlight>
{{out}}
<pre>
Largest 5 adjacent digits are 99914
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
for i to 1000
n$ &= random 10 - 1
.
min = 99999
for i = 1 to 995
n = number substr n$ i 5
min = lower min n
max = higher max n
.
print min & " " & max
</syntaxhighlight>
{{out}}
<pre>
21 99768
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping io math.functions prettyprint random sequences ;
 
1000 10^ random unparse 5 <clumps> supremum print</langsyntaxhighlight>
{{out}}
<pre>
99987
</pre>
 
=={{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.
<syntaxhighlight lang="freebasic">
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
</syntaxhighlight>
{{output}}
<pre>
99748 31
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="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
}
}
}</syntaxhighlight>
 
{{out}}
Sample run:
<pre>
The largest number formed from 5 adjacent digits (99928) is: 99,928
The smallest number formed from 5 adjacent digits (00120) is: 120
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">>./5([+10*])/@|:\?1000#10</syntaxhighlight>
{{out}}
(example)
<pre>99929</pre>
 
=={{header|jq}}==
{{works with|jq}}
 
'''Also works with gojq, the Go implementation of jq.'''
 
First, a direct solution using only jq's standard library and a line for generating the PRN:
<syntaxhighlight lang="bash">
< /dev/random tr -cd '0-9' | head -c 1000 | jq -R '
length as $n
| . as $s
| ($s[0:5] | tonumber) as $m
| reduce range(1; $n - 5) as $i ( {min: $m, max: $m};
($s[$i: $i+5] | tonumber) as $x
| if $x < .min then .min = $x
elif $x > .max then .max = $x
else . end)
'
</syntaxhighlight>
{{output}}
<pre>
{
"min": 224,
"max": 99772
}
</pre>
Next, a "one-line solution" apart from generic helper functions and the line for generating the PRN:
<syntaxhighlight lang="bash">
< /dev/random tr -cd '0-9' | head -c 1000 | jq -R '
# Input: an array
# Output: a stream of the width-long subarrays
def windows(width):
range(0; 1 + length - width) as $i | .[$i:$i+width];
 
def minmax(s):
reduce s as $x ( {};
if .min == null then {min: $x, max: $x}
elif $x < .min then .min = $x
elif $x > .max then .max = $x else . end);
 
explode | minmax(windows(5) | implode | tonumber)
</syntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">dig = rand(0:9, 1000)
@show maximum(evalpoly(10, dig[i:i+4]) for i in 1:length(dig)-4)
</langsyntaxhighlight>{{out}}
<pre>
maximum((evalpoly(10, dig[i:i + 4]) for i = 1:length(dig) - 4)) = 99993
Line 68 ⟶ 666:
Maximum is 99999 at position 763.
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">MinMax[FromDigits /@ Partition[RandomInteger[{0, 9}, 1000], 5, 1]]</syntaxhighlight>
 
{{out}}<pre>{104,99984}</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="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()</syntaxhighlight>
 
{{out}}
<pre>Largest 5-digit number extracted from random 1000-digit number: 99855</pre>
 
=={{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.
<langsyntaxhighlight lang="pascal">
var
digits,
Line 88 ⟶ 723:
end;
writeln(s, ' found as largest 5 digit number ')
end.</langsyntaxhighlight>
{{out}}
<pre>99889 found as largest 5 digit number </pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Largest_five_adjacent_number
Line 100 ⟶ 735:
$_ = join '', map int rand 10, 1 .. 1e3;
my @n;
$@n[$1] = $1 while /(?=(\d{5}))/g ] = ();
print "$#n[-1]\n";</langsyntaxhighlight>
{{out}}
<pre>
99899
99958
</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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: #004080;">string</span> <span style="color: #000000;">hi5</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">5</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">lo5</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hi5</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">4</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s5</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">hi5</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hi5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s5</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">lo5</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lo5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s5</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"String %s: hi5:%s, lo5:%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">hi5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lo5</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1001</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (set precision to 1,000 dp, plus the "3.")</span>
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
String 35369847249221789712...55814915156742014134 (1,000 digits): hi5:99969, lo5:00013
String 14159265358979323846...66111959092164201989 (1,000 digits): hi5:99999, lo5:00031
</pre>
 
=={{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.
<syntaxhighlight lang="python">
#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)
</syntaxhighlight>
Results from multiple runs :
{{out}}
<pre>
 
 
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
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> 9 random 1+
999 times [ 10 * 10 random + ]
dup 10 995 ** / swap
say "1000 digit number"
cr cr
dup echo
cr cr
[] swap 1000 times
[ 10 /mod
rot join swap ]
drop dup
0 0 rot witheach
[ swap 10 *
100000 mod +
tuck max swap ]
drop
say "largest 5 adjacent digits " echo
cr cr
5 split nip
dip dup witheach
[ swap 10 *
100000 mod +
tuck min swap ]
drop
say "smallest 5 adjacent digits "
number$
char 0 over size 5 swap - of
swap join echo$</syntaxhighlight>
 
{{out}}
 
<pre>1000 digit number
 
6840907174710710253578773992410923828010161316527489025709598588564725782830158923520744533291356763925463645174705745049218864529135157750600471363289558510223445011025163844199130052941524405130793922050669143532883592357897096269697903780509770222546659289832999639637730759831717125055857319129937934353617386529810429642261048827016148476352187592939822910964334104828550764225596939965675519243696921514153715258715961987394884393797714002723369560598384723111928648279375269590756880538160907807290640466592734345970439851284217252141914792365031610947925633607292897379320456985054219371373707477609843617810620097343420379245258762479642377134776965386535533204636182773979582543243782455626021964121509778973939346873293400502531060571761381532229278485105166678017234489439222625767334040651185482277484204647473910364297105035077787620562600454016296114868335345408156093266755340971022669397814048919735693462065796634326535292979494128432997646841467835174156471055078228524511787150409
 
largest 5 adjacent digits 99963
 
smallest 5 adjacent digits 00272</pre>
 
=={{header|Raku}}==
Line 117 ⟶ 877:
Do it 5 times for variety, it's random after all.
 
<syntaxhighlight lang="raku" perl6line>(^௰).roll(௲).rotor(5 => -4)».join.minmax.bounds.put xx 5</langsyntaxhighlight>
{{out|Sample output}}
<pre>00371 99975
Line 127 ⟶ 887:
=={{header|Ring}}==
 
<langsyntaxhighlight lang="ring">
digit = ""
max = 0
maxOldmin = 099999
limit = 1000
 
Line 142 ⟶ 902:
res = substr(digit,n,5)
resNum = number(res)
if resNum > maxoldmax
max = resNum
maxOld = maxok
if resNum < min
min = res
ok
next
 
see "The largest number is:" + nl
see max + nl
see "The smallest number is:" + nl
</lang>
see min + nl
</syntaxhighlight>
{{out}}
<pre>
The largest number is:
99638
The smallest number is:
00118
</pre>
 
=={{header|RPL}}==
RPL can not handle 1000-digit numbers, so we use a 1000-digit string.
≪ ""
1 1000 '''START''' RAND 9 * 0 RND →STR + '''NEXT'''
{ -99999 0 }
1 3 PICK SIZE 4 - '''FOR''' j
OVER j DUP 4 + SUB
STR→ NEG LASTARG 2 →LIST MAX
'''NEXT''' ABS
≫ '<span style="color:blue>TASK</span>' STO
{{out}}
<pre>
2: "46725324552811522…
1: { 198 99886 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="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)
</syntaxhighlight>
{{out}}
<pre>minimum sequence 00096, maximum sequence 99508.
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">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)</syntaxhighlight>
{{out}}
<pre>
length(n) = 1000
Min 5-digit sub-number: 00072
Max 5-digit sub-number: 99861
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (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
}
}
}</syntaxhighlight>
{{out}}
<pre>The largest number formed from 5 adjacent digits (99928) is: 99,928
The smallest number formed from 5 adjacent digits (00120) is: 120</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
Very simple approach as there's little need for speed here.
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
Line 166 ⟶ 1,008:
for (i in 0...999) digits[i] = rand.int(10)
var number = digits.join()
for (ir in [99999...0, 0..99999]) {
var quintettarget = Fmt.swrite(r.from == 0) ? "$05dsmallest", i): "largest "
iffor (number.contains(quintet)i in r) {
var quintet = Fmt.printswrite("The largest number formed from 5 adjacent digits is: $,d05d", i)
returnif (number.contains(quintet)) {
Fmt.print("The $s number formed from 5 adjacent digits ($s) is: $,6d", target, quintet, i)
break
}
}
}</langsyntaxhighlight>
 
{{out}}
Sample output:
<pre>
The largest number formed from 5 adjacent digits (99830) is: 99,850830
The smallest number formed from 5 adjacent digits (00154) is: 154
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">char Number(1000);
int Num, Max, I, J;
[for I:= 0 to 1000-1 do \generate 1000-digit number
Line 194 ⟶ 1,040:
];
IntOut(0, Max);
]</langsyntaxhighlight>
 
{{out}}
1,983

edits