Numbers with equal rises and falls

From Rosetta Code
Revision as of 00:16, 10 October 2020 by Not a robot (talk | contribs) (Add Forth)
Numbers with equal rises and falls 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.

When a number is written in base 10, adjacent digits may "rise" or "fall" as the number is read (usually from left to right).
OEIS Sequence A296712 describes numbers whose digit sequence in base 10 have equal "rises" and "falls".
Definition:
Given the digits of the number are written as a series d:

  • A rise is an index i such that d(i) < d(i+1)
  • A fall is an index i such that d(i) > d(i+1).


Examples:

  • The number 726169 has 3 rises and 2 falls, so it is not in the sequence.
  • The number 83548 has 2 rises and 2 falls, so it is in the sequence.


Task

Print the first 200 numbers in the sequence. Show that the 10 millionth (10,000,000th) number in the sequence is 41909002.

See also: OEIS:A296712 the Oeis entry.

8080 Assembly

<lang 8080asm>puts: equ 9 ; CP/M calls putch: equ 2 org 100h ;;; Print first 200 numbers lxi d,first mvi c,puts call 5 mvi b,200 ; 200 numbers f200: push b call next ; Get next number call pnum ; Print the number pop b ; Restore counter dcr b ; Are we there yet? jnz f200 ; If not, next number ;;; Find 10,000,000th number lxi d,tenmil mvi c,puts call 5 f1e7: call next ; Keep generating numbers until ten million reached jnz f1e7 ; Then print the number ;;; Print the current number pnum: lxi d,num pscan: dcx d ; Scan for zero ldax d ana a jnz pscan mvi c,puts ; Once found, print string jmp 5 ;;; Increment number until rises and falls are equal next: lxi h,num incdgt: mov a,m ; Get digit ana a ; If 0, then initialize jz grow inr a ; Otherwise, increment mov m,a ; Store back cpi '9'+1 ; Rollover? jnz idone ; If not, we're done mvi m,'0' ; If so, set digit to 0 dcx h ; And increment previous digit jmp incdgt grow: mvi m,'1' idone: lxi h,num ; Find rises and falls mvi b,0 ; B = rises - falls mov c,m ; C = right digit in comparison pair: dcx h mov a,m ; A = left digit in comparison ana a ; When zero, done jz check cmp c ; Compare left digit to right digit jc fall ; A<C = fall jnz rise ; A>C = rise nxdgt: mov c,a ; C is now left digit jmp pair ; Check next pair fall: dcr b ; Fall: decrement B jmp nxdgt rise: inr b ; Rise: increment B jmp nxdgt check: mov a,b ; If B=0 then rises and falls are equal ana a jnz next ; Otherwise, increment number and try again lxi h,ctr ; But if so, decrement the counter to 10 million mov a,m ; First byte sui 1 mov m,a inx h ; Second byte mov a,m sbb b ; B=0 here mov m,a inx h ; Third byte mov a,m sbb b mov m,a dcx h ; OR them together to see if the number is zero ora m dcx h ora m ret ;;; Strings first: db 'The first 200 numbers are:',13,10,'$' tenmil: db 13,10,10,'The 10,000,000th number is: $' ;;; Current number (stored as ASCII) db 0,0,0,0,0,0,0,0 num: db '0 $' ;;; 24-bit counter to keep track of ten million ctr: db 80h,96h,98h ; 1e7 = 989680h</lang>

Output:
The first 200 numbers are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404 

The 10,000,000th number is: 41909002 

8086 Assembly

<lang asm>puts: equ 9 ; MS-DOS print string cpu 8086 bits 16 org 100h section .text mov bp,98h ; BP:DI = 989680h = ten million mov di,9680h ;;; Print first 200 numbers mov dx,first ; Print message mov ah,puts int 21h n200: call next ; Get next number call pnum ; Print the number cmp di,95B8h ; Have we had 200 yet? ja n200 ; If not, print next number ;;; Print the 10 millionth number mov dx,tenmil ; Print message mov ah,puts int 21h n1e7: call next ; Get next number jnz n1e7 ; Until we have the 10 millionth ;;; Print the current number pnum: std ; Read backwards xchg si,di ; Keep DI safe mov di,num mov cx,9 xor al,al ; Find the first zero repnz scasb inc di ; Go to first digit inc di xchg si,di ; Put DI back mov dx,si ; Call DOS to print the number mov ah,puts int 21h ret ;;; Increment number until rises and falls are equal next: std ; Read number backwards .inc: mov bx,num .iloop: mov al,[bx] ; Get digit test al,al ; If uninitialized, write a 1 jz .grow inc ax ; Otherwise, increment mov [bx],al ; Write it back cmp al,'9'+1 ; Rollover? jnz .idone ; If not, the increment is done mov [bx],byte '0' ; But if so, this digit should be 0, dec bx ; and the next digit incremented. jmp .iloop .grow: mov [bx],byte '1' ; The number gains an extra digit .idone: xor bl,bl ; BL = rise and fall counter mov si,num lodsb ; Read first digit to compare to .pair: xchg ah,al ; Previous digit to compare lodsb ; Read next digit test al,al ; Done yet? jz .done cmp al,ah ; If not, compare the digits ja .fall ; If they are different, jb .rise ; there is a fall or a rise jmp .pair ; Otherwise, try next pair .fall: dec bl ; Fall: decrement BL jmp .pair .rise: inc bl ; Rise: increment BL jmp .pair .done: test bl,bl ; At the end, check if BL is zero jnz .inc ; If not, try next number sub di,1 ; Decrement the million counter in BP:DI sbb bp,0 mov ax,di ; Test if BP:DI is zero or ax,bp ret section .data ;;; Strings first: db 'The first 200 numbers are:',13,10,'$' tenmil: db 13,10,10,'The 10,000,000th number is: $' ;;; Current number, stored as ASCII db 0,0,0,0,0,0,0,0 num: db '0 $'</lang>

Output:
The first 200 numbers are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10,000,000th number is: 41909002 


ALGOL 68

Translation of: Wren

... with a single counter for rises and falls.

<lang algol68>BEGIN

   # returns TRUE if the number of digits in n followed by a higher digit (rises)  #
   #              equals the number of digits followed by a lower digit (falls)    #
   #        FALSE otherwise                                                        #
   PROC rises equals falls = ( INT n )BOOL:
        BEGIN
           INT rf   := 0;
           INT prev := n MOD 10;
           INT v    := n OVER 10;
           WHILE v > 0 DO
               INT d = v MOD 10;
               IF d < prev THEN
                   rf +:= 1    # rise                                              #
               ELIF d > prev THEN 
                   rf -:= 1    # fall                                              #
               FI;
               prev := d;
               v OVERAB 10
           OD;
           rf = 0
       END; # rises equals falls #
   # task tests                                                                    # 
   print( ( "The first 200 numbers in the sequence are:", newline ) );
   INT count    := 0;
   INT max count = 10 000 000;
   FOR n WHILE count < max count DO
       IF rises equals falls( n ) THEN
           count +:= 1;
           IF count <= 200 THEN
               print( ( whole( n, -4 ) ) );
               IF count MOD 20 = 0 THEN print( ( newline ) ) FI
           ELIF count = max count THEN
               print( ( newline, "The 10 millionth number in the sequence is ", whole( n, -8 ), ".", newline ) )
           FI
       FI
   OD

END </lang>

Output:
The first 200 numbers in the sequence are:
   1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102
 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152
 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184
 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208
 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252
 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284
 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308
 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342
 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380
 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10 millionth number in the sequence is 41909002.

APL

Works with: Dyalog APL

<lang APL>risefall←{

   ⍝ Determine if a number is in the sequence
   inSeq←0=(+/2(<->)/10(⊥⍣¯1)⊢)
   ⍝ First 200 numbers
   ⎕←'The first 200 numbers are:'
   ⎕←(⊢(/⍨)inSeq¨)⍳404
   ⍝ 10,000,000th number
   ⍝ You can't just make a list that big and filter
   ⍝ it, because that will just get you a WS FULL.
   ⍝ Instead it's necessary to loop over them the old-
   ⍝ fashioned way
   ⍞←'The 10,000,000th number is: '
   ⎕←1e7{⍺=0:⍵-1 ⋄ (⍺-inSeq ⍵)∇ ⍵+1}1

}</lang>

Output:
The first 200 numbers are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102
      103 104 105 106 107 108 109 111 120 121 130 131
      132 140 141 142 143 150 151 152 153 154 160 161
      162 163 164 165 170 171 172 173 174 175 176 180
      181 182 183 184 185 186 187 190 191 192 193 194
      195 196 197 198 201 202 203 204 205 206 207 208
      209 212 213 214 215 216 217 218 219 222 230 231
      232 240 241 242 243 250 251 252 253 254 260 261
      262 263 264 265 270 271 272 273 274 275 276 280
      281 282 283 284 285 286 287 290 291 292 293 294
      295 296 297 298 301 302 303 304 305 306 307 308
      309 312 313 314 315 316 317 318 319 323 324 325
      326 327 328 329 333 340 341 342 343 350 351 352
      353 354 360 361 362 363 364 365 370 371 372 373
      374 375 376 380 381 382 383 384 385 386 387 390
      391 392 393 394 395 396 397 398 401 402 403 404
The 10,000,000th number is: 
41909002

C

<lang C>#include <stdio.h>

/* Check whether a number has an equal amount of rises

* and falls
*/

int riseEqFall(int num) {

   int rdigit = num % 10;
   int netHeight = 0;
   while (num /= 10) {
       netHeight += ((num % 10) > rdigit) - ((num % 10) < rdigit);
       rdigit = num % 10;
   }
   return netHeight == 0;

}

/* Get the next member of the sequence, in order,

* starting at 1
*/

int nextNum() {

   static int num = 0;
   do {num++;} while (!riseEqFall(num));
   return num;

}

int main(void) {

   int total, num;
   
   /* Generate first 200 numbers */
   printf("The first 200 numbers are: \n");
   for (total = 0; total < 200; total++)
       printf("%d ", nextNum());
   
   /* Generate 10,000,000th number */
   printf("\n\nThe 10,000,000th number is: ");
   for (; total < 10000000; total++) num = nextNum();
   printf("%d\n", num);
   
   return 0;

}</lang>

Output:
The first 200 numbers are: 
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404 

The 10,000,000th number is: 41909002

F#

<lang fsharp> // A296712. Nigel Galloway: October 9th., 2020 let fN g=let rec fN Ψ n g=match n,Ψ with (0,0)->true |(0,_)->false |_->let i=n%10 in fN (Ψ + (compare i g)) (n/10) i in fN 0 g (g%10) let A296712=seq{1..2147483647}|>Seq.filter fN A296712|>Seq.take 200|>Seq.iter(printf "%d "); printfn"\n" [999999;9999999;99999999]|>List.iter(fun n->printfn "The %dth element is %d" (n+1) (Seq.item n A296712)) </lang>

Output:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 1000000th element is 3284698
The 10000000th element is 41909002
The 100000000th element is 375551037

Factor

Works with: Factor version 0.99 2020-08-14

<lang factor>USING: grouping io kernel lists lists.lazy math math.extras prettyprint tools.memory.private ;

rises-and-falls-equal? ( n -- ? )
   0 swap 10 /mod swap
   [ 10 /mod rot over - sgn rotd + spin ] until-zero drop 0 = ;
OEIS:A296712 ( -- list )
   1 lfrom [ rises-and-falls-equal? ] lfilter ;

! Task "The first 200 numbers in OEIS:A296712 are:" print 200 OEIS:A296712 ltake list>array 20 group simple-table. nl

"The 10 millionth number in OEIS:A296712 is " write OEIS:A296712 9,999,999 [ cdr ] times car commas print</lang>

Output:
The first 200 numbers in OEIS:A296712 are:
1   2   3   4   5   6   7   8   9   11  22  33  44  55  66  77  88  99  101 102
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10 millionth number in OEIS:A296712 is 41,909,002

Forth

<lang forth>: in-seq? ( n -- is N in the sequence? )

 0 swap            \ height 
 10 /mod           \  digit and rest of number 
 begin dup while   \ as long as the number isn't zero... 
   10 /mod         \ get next digit and quotient
   -rot swap       \ retrieve previous digit 
   over - sgn      \ see if higher, lower or equal (-1, 0, 1) 
   >r rot r> +     \ add to height
   -rot swap       \ quotient on top of stack 
 repeat
 drop drop         \ drop number and last digit 
 0=                \ is height equal to zero? 
next-val ( n -- n: retrieve first element of sequence higher than N )
 begin 1+ dup in-seq? until
two-hundred
 begin over 200 < while 
   next-val dup . 
   swap 1+ swap
 repeat  
ten-million
 begin over 10000000 < while 
   next-val
   swap 1+ swap
 repeat

0 0 \ top of stack: current index and number ." The first 200 numbers are: " two-hundred cr cr ." The 10,000,000th number is: " ten-million . cr bye</lang>

Output:
The first 200 numbers are: 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10,000,000th number is: 41909002 

Go

Translation of: Wren

<lang go>package main

import "fmt"

func risesEqualsFalls(n int) bool {

   if n < 10 {
       return true
   }
   rises := 0
   falls := 0
   prev := -1
   for n > 0 {
       d := n % 10
       if prev >= 0 {
           if d < prev {
               rises = rises + 1
           } else if d > prev {
               falls = falls + 1
           }
       }
       prev = d
       n /= 10   
   }
   return rises == falls

}

func main() {

   fmt.Println("The first 200 numbers in the sequence are:")
   count := 0
   n := 1
   for {
       if risesEqualsFalls(n) {
           count++
           if count <= 200 {
               fmt.Printf("%3d ", n)
               if count%20 == 0 {
                   fmt.Println()
               }
           }
           if count == 1e7 {
               fmt.Println("\nThe 10 millionth number in the sequence is ", n)
               break
           }
       }
       n++
   }

}</lang>

Output:
The first 200 numbers in the sequence are:
  1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102 
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404 

The 10 millionth number in the sequence is  41909002

Haskell

<lang haskell>import Data.Char

pairs :: [a] -> [(a,a)] pairs (a:b:as) = (a,b):pairs (b:as) pairs _ = []

riseEqFall :: Int -> Bool riseEqFall n = rel (>) digitPairs == rel (<) digitPairs

   where rel r = sum . map (fromEnum . uncurry r)
         digitPairs = pairs $ map digitToInt $ show n

a296712 :: [Int] a296712 = [n | n <- [1..], riseEqFall n]

main :: IO () main = do putStrLn "The first 200 numbers are: " putStrLn $ unwords $ map show $ take 200 a296712 putStrLn "" putStr "The 10,000,000th number is: " putStrLn $ show $ a296712 !! 9999999 </lang>

Output:
The first 200 numbers are: 
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10,000,000th number is: 41909002

Julia

<lang julia>using Lazy

function rises_and_falls(n)

   if n < 10
       return 0, 0
   end
   lastr, rises, falls = n % 10, 0, 0
   while n != 0
       n, r = divrem(n, 10)
       if r > lastr
           falls += 1
       elseif r < lastr
           rises += 1
       end
       lastr = r
   end
   return rises, falls

end

isA296712(x) = ((a, b) = rises_and_falls(x); return a == b)

function genA296712(N, M)

   A296712 = filter(isA296712, Lazy.range(1));
   arr = take(N, A296712)
   j = 0
   for i in take(200, A296712)
       j += 1
       print(lpad(i, 4), j % 20 == 0 ? "\n" : "")
   end
   for i in take(M, A296712)
       j = i
   end
   println("\nThe $M-th number in sequence A296712 is $j.")

end

genA296712(200, 10_000_000)

</lang>

Output:
   1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102
 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152
 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184
 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208
 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252
 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284
 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308
 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342
 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380
 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10000000-th number in sequence A296712 is 41909002.

Phix

<lang Phix>atom t1 = time()+1 integer count = 0, n = 0 printf(1,"The first 200 numbers are:\n") while true do

   n += 1
   integer rmf = 0,
           l = remainder(n,10),
           r = floor(n/10)
   while r do
       integer p = remainder(r,10)
       rmf += compare(l,p)
       l = p
       r = floor(r/10)
   end while
   if rmf=0 then
       count += 1
       if count<=200 then
           printf(1,"%3d ",n)
           if remainder(count,20)=0 then
               printf(1,"\n")
           end if
       end if
       if count == 1e7 then
           progress("")
           printf(1,"\nThe %,dth number is %,d\n",{count,n})
           exit
       end if
       if time()>t1 then
           progress("%,d:%,d\r",{count,n})
           t1 = time()+1
       end if
   end if

end while</lang>

Output:
The first 200 numbers are:
  1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10,000,000th number is 41,909,002

Python

<lang python>import itertools

def riseEqFall(num):

   """Check whether a number belongs to sequence A296712."""
   height = 0
   d1 = num % 10
   num //= 10
   while num:
       d2 = num % 10
       height += (d1<d2) - (d1>d2)
       d1 = d2
       num //= 10
   return height == 0
   

def sequence(start, fn):

   """Generate a sequence defined by a function"""
   num=start-1
   while True:
       num += 1
       while not fn(num): num += 1
       yield num

a296712 = sequence(1, riseEqFall)

  1. Generate the first 200 numbers

print("The first 200 numbers are:") print(*itertools.islice(a296712, 200))

  1. Generate the 10,000,000th number

print("The 10,000,000th number is:") print(*itertools.islice(a296712, 10000000-200-1, 10000000-200))

  1. It is necessary to subtract 200 from the index, because 200 numbers
  2. have already been consumed.

</lang>

Output:
The first 200 numbers are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404
The 10,000,000th number is:
41909002


Raku

Works with: Rakudo version 2020.09

<lang perl6>use Lingua::EN::Numbers; use Base::Any;

sub rf (int $base = 10, $batch = Any) {

   my %batch = batch => $batch if $batch;
   lazy flat 1 ..^ $base, ($base .. ∞).hyper(|%batch).map: {
       my int ($this, $last) = $_, $_ % $base;
       my int ($rise, $fall) = 0, 0;
       while $this {
           my int $rem = $this % $base;
           $this = $this div $base;
           if    $rem > $last { $fall = $fall + 1 }
           elsif $rem < $last { $rise = $rise + 1 }
           $last = $rem
       }
       next unless $rise == $fall;
       $_
   }

}

my $upto = 200; put "First $upto (base 10):"; .put for rf[^$upto]».fmt("%3d").batch(20);

$upto = 10_000_000; put "\nThe {$upto.&comma}th (base 10): ", comma rf(10, 65536)[$upto - 1];

$upto = ^6; put "\n{tc $upto.map({.exp(10).&ordinal}).join: ', '}, values in some other bases:"; for 2, 'A005408',

   3,  'A296691',
   4,  'A296694',
   5,  'A296697',
   6,  'A296700',
   7,  'A296703',
   8,  'A296706',
   9,  'A296709',
   10, 'A296712',
   11, 'A296744',
   12, 'A296747',
   13, 'A296750',
   14, 'A296753',
   15, 'A296756',
   16, 'A296759',
   20, 'A296762',
   60, 'A296765'
 -> $base, $oeis {
   put "Base {$base.fmt(<%2d>)} (OEIS:$oeis): ",
   $upto.map({rf($base, 5000)[.exp(10) - 1].&to-base($base)}).join: ', '

}</lang>

Output:
First 200 (base 10):
  1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404

The 10,000,000th (base 10): 41,909,002

First, tenth, one hundredth, one thousandth, ten thousandth, one hundred thousandth, values in some other bases:
Base  2 (OEIS:A005408): 1, 10011, 11000111, 11111001111, 100111000011111, 110000110100111111
Base  3 (OEIS:A296691): 1, 201, 22112, 10101111, 1100022001, 112010220010
Base  4 (OEIS:A296694): 1, 111, 3333, 221012, 13002120, 1023032003
Base  5 (OEIS:A296697): 1, 102, 1441, 40011, 1431201, 40142240
Base  6 (OEIS:A296700): 1, 55, 512, 20424, 400402, 10405133
Base  7 (OEIS:A296703): 1, 44, 365, 12620, 155554, 2525215
Base  8 (OEIS:A296706): 1, 33, 316, 7466, 60404, 1336621
Base  9 (OEIS:A296709): 1, 22, 275, 5113, 40217, 665836
Base 10 (OEIS:A296712): 1, 11, 252, 3396, 29201, 393329
Base 11 (OEIS:A296744): 1, A, 216, 2240, 21718, 217785
Base 12 (OEIS:A296747): 1, A, 201, 10AA, 19723, B3025
Base 13 (OEIS:A296750): 1, A, 1B8, A0A, 172A7, 823C8
Base 14 (OEIS:A296753): 1, A, 1B5, 8B9, 14B81, 6476D
Base 15 (OEIS:A296756): 1, A, 1B2, 7D4, 11BBA, 51013
Base 16 (OEIS:A296759): 1, A, 1A9, 716, 10424, 41E9D
Base 20 (OEIS:A296762): 1, A, 196, 523, 8011, 24HE2
Base 60 (OEIS:A296765): 1, A, ff, 1f2, 63Q, g5D

REXX

To do the heavy lifting,   this REXX program constructs a table of every two-digit sequence which indicates a
rise   (+1),     fall   (-1),     or   neither   (0). <lang rexx>/*REXX pgm finds and displays N numbers that have an equal number of rises and falls,*/ parse arg n . /*obtain optional argument from the CL.*/ if n== | n=="," then n= 200 /*Not specified? Then use the default.*/ append= n>0 /*a flag that is used to append numbers*/ n= abs(n) /*use the absolute value of N. */ call init /*initialize the rise/fall database. */

         do j=1  until #==n;  Lm= length(j) - 1
         s= 0                                   /*initialize the sum of  rises/falls.  */
              do k=1  for Lm;  t= substr(j,k,2) /*obtain a set of two digs from number.*/
              s= s + @.t                        /*sum the rises and falls in the number*/
              end   /*k*/
         if s\==0  then iterate                 /*Equal # of rises & falls? Then add it*/
         #= # + 1                               /*bump the count of numbers found.     */
         if append  then $= $ j                 /*append to the list of numbers found. */
                    else $= j                   /*merely set the last number found.    */
         end   /*j*/

if append then call show /*display a list of N numbers──►term.*/

          else say  'the '  commas(n)th(n)  " number is: "   commas($)    /*show Nth #.*/

exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ init: @.= 0; do i=1 for 9; _= i' '; @._= 1; _= '0'i; @._= +1; end /*i*/

              do i=10  to 99;   parse var i  a 2 b;     if a>b  then              @.i= -1
                                                                else if a<b  then @.i= +1
              end   /*i*/;      #= 0;        $=;        return

/*──────────────────────────────────────────────────────────────────────────────────────*/ show: say 'the first ' commas(#) " numbers are:"; say; w= length( word($, #) )

     _=;    do o=1  for n;     _= _ right( word($, o), w);    if o//20\==0  then iterate
            say substr(_, 2);  _=               /*display a line;  nullify a new line. */
            end   /*o*/                         /* [↑]  display  20  numbers to a line.*/
     if _\==  then say substr(_, 2);   return /*handle any residual numbers in list. */

/*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg _; do c=length(_)-3 to 1 by -3; _=insert(',', _, c); end; return _ th: parse arg th; return word('th st nd rd',1+(th//10)*(th//100%10\==1)*(th//10<4))</lang>

output   when using the default input:
the first  200  numbers are:

  1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404
output   when using the input of:     -10000000
the  10,000,000th  number is:  41,909,002

Wren

Library: Wren-fmt

<lang ecmascript>import "/fmt" for Fmt

var risesEqualsFalls = Fn.new { |n|

   if (n < 10) return true
   var rises = 0
   var falls = 0
   var prev = -1
   while (n > 0) {
       var d = n%10
       if (prev >= 0) {
           if (d < prev) {
               rises = rises + 1
           } else if (d > prev) {
               falls = falls + 1
           }
       }
       prev = d
       n = (n/10).floor
   }
   return rises == falls

}

System.print("The first 200 numbers in the sequence are:") var count = 0 var n = 1 while (true) {

   if (risesEqualsFalls.call(n)) {
       count = count + 1
       if (count <= 200) {
           Fmt.write("$3d ", n)
           if (count % 20 == 0) System.print()
       }
       if (count == 1e7) {
           Fmt.print("\nThe 10 millionth number in the sequence is $,d.", n)
           break
       }
   }
   n = n + 1

}</lang>

Output:
The first 200 numbers in the sequence are:
  1   2   3   4   5   6   7   8   9  11  22  33  44  55  66  77  88  99 101 102 
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404 

The 10 millionth number in the sequence is 41,909,002.