Numbers with equal rises and falls
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.
ALGOL 68
... 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.
Factor
<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
Go
<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
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
Raku
<lang perl6>use Lingua::EN::Numbers;
my @rf = lazy flat 1..9, (10..∞).hyper(:65536batch).map: {
my int ($this, $last) = $_, $_ % 10; my int ($rise, $fall) = 0, 0; while $this { my int $rem = $this % 10; $this = $this div 10; if $rem > $last { $fall = $fall + 1 } elsif $rem < $last { $rise = $rise + 1 } $last = $rem }; next unless $rise == $fall; $_
};
put "First 200:"; .put for @rf[^200]>>.fmt("%3d").batch(20);
put "\nThe {10000000.&comma}th: ", comma @rf[9_999_999]</lang>
- Output:
First 200: 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: 41,909,002
REXX
<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 /*initilize 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 ' th(n) " number is: " $ /*display the Nth number.*/
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 ' # " 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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/ th: parse arg th; return th||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 22
the 10000000th number is: 41909002
Wren
<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.