Numbers with equal rises and falls: Difference between revisions

Add 8086 assembly
(Add Python)
(Add 8086 assembly)
Line 116:
<pre>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 </pre>
 
=={{header|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>
 
{{out}}
 
<pre>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 </pre>
2,114

edits