Jump to content

Numbers with equal rises and falls: Difference between revisions

Add 8080 assembly
(→‎{{header|Haskell}}: fix off by one)
(Add 8080 assembly)
Line 23:
See also:
[[OEIS:A296712]] the Oeis entry.
 
=={{header|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>
 
{{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>
 
 
=={{header|ALGOL 68}}==
2,121

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.