Largest five adjacent number

Revision as of 06:14, 1 October 2021 by CalmoSoft (talk | contribs)


Generate random 1000-digit number.
Find the five adjacent digits in the 1000-digit number that form the largest 5-digit number.

Largest five adjacent number 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
Extra credi


Find the five adjacent digits in the 1000-digit number that form the smallest 5-digit number.


ALGOL 68

Adding the minimum number for good measure... <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;
   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</lang>

Output:
Largest  5 consecutive digits from 1000 random digits: 99987
Smallest 5 consecutive digits from 1000 random digits: 00119

APL

Works with: Dyalog APL

<lang apl>⌈/((⊣+10×⊢)/(⌽↓))⌺5⊢(-⎕IO)+?1000/10</lang>

Output:

(example)

99994

BQN

<lang bqn>⌈´(⊣+10×⊢)˝⌽⍉5↕1000 •rand.Range 10</lang>

Output:

(example)

99991

C

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <stdint.h>
  3. include <time.h>
  1. define DIGITS 1000
  2. 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;

}</lang>

Output:

(example)

99931

F#

<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)}" </lang>

Output:
Largest 5 adjacent digits are 99914

Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: grouping io math.functions prettyprint random sequences ;

1000 10^ random unparse 5 <clumps> supremum print</lang>

Output:
99987

Go

Translation of: Wren
Library: Go-rcu

<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 is: %s\n", ci)
           return
       }
   }

}</lang>

Output:

Sample run:

The largest number formed from 5 adjacent digits is: 99,827

J

<lang j>>./5([+10*])/@|:\?1000#10</lang>

Output:

(example)

99929

Julia

<lang julia>dig = rand(0:9, 1000) @show maximum(evalpoly(10, dig[i:i+4]) for i in 1:length(dig)-4)

</lang>

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.

Nim

<lang Nim>import random, strutils

randomize()

const N = 1000 type Digit = 0..9

  1. 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()</lang>

Output:
Largest 5-digit number extracted from random 1000-digit number: 99855

Pascal

Works with: Free 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.

<lang pascal> 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.</lang>

Output:
99889 found as largest 5 digit number 

Perl

<lang 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";</lang>

Output:
99899

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.

<lang perl6>(^௰).roll(௲).rotor(5 => -4)».join.minmax.bounds.put xx 5</lang>

Sample output:
00371 99975
00012 99982
00008 99995
00012 99945
00127 99972

Ring

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

Output:
The largest number is:
99638
The smallest number is:
00118

Wren

Library: Wren-fmt

Very simple approach as there's little need for speed here. <lang ecmascript>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 (i in 99999...0) {

   var quintet = Fmt.swrite("$05d", i)
   if (number.contains(quintet)) {
       Fmt.print("The largest number formed from 5 adjacent digits is: $,d", i)
       return
   }

}</lang>

Output:

Sample output:

The largest number formed from 5 adjacent digits is: 99,850

XPL0

<lang 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); ]</lang>

Output:
99930