Numbers with equal rises and falls: Difference between revisions

m
(Add MAD)
 
(38 intermediate revisions by 26 users not shown)
Line 1:
{{draft task}}
When a number is written in base 10, adjacent digits may "rise" or "fall"
as the number is read (usually from left to right).
<br>
OEIS Sequence A296712 describes numbers whose digit sequence in base 10 have equal "rises" and "falls".
<br>
Definition:
<br>
Given the digits of the number are written as a series d:<br>
:* 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).
<br>
Examples:<br>
:* 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.
<br>
 
When a number is written in base 10, &nbsp; adjacent digits may "rise" or "fall" as the number is read &nbsp; (usually from left to right).
;Task
 
 
;Definition:
Given the decimal digits of the number are written as a series &nbsp; <big>d</big>:
:* &nbsp; A &nbsp; ''rise'' &nbsp; is an index &nbsp; <big>i</big> &nbsp; such that &nbsp; <big> d(i) &nbsp;&lt;&nbsp; d(i+1)</big>
:* &nbsp; A &nbsp; ''fall''&nbsp; &nbsp; is an index &nbsp; <big>i</big> &nbsp; such that &nbsp; <big> d(i) &nbsp;&gt;&nbsp; d(i+1)</big>
 
 
;Examples:
:* &nbsp; The number &nbsp; '''726,169''' &nbsp; has &nbsp; '''3''' &nbsp; rises and &nbsp; '''2''' &nbsp; falls, &nbsp; so it <u>isn't</u> in the sequence.
:* &nbsp; The number &nbsp; &nbsp; '''83,548''' &nbsp; has &nbsp; '''2''' &nbsp; rises and &nbsp; '''2''' &nbsp; falls, &nbsp; so it &nbsp; <u>is</u> &nbsp; in the sequence.
 
 
;Task:
:* &nbsp; Print the first &nbsp; '''200''' &nbsp; numbers in the sequence
:* &nbsp; Show that the &nbsp; '''10 millionth''' &nbsp; (10,000,000<sup>th</sup>) &nbsp; number in the sequence is &nbsp; '''41,909,002'''
 
Print the first 200 numbers in the sequence. Show that the 10 millionth
(10,000,000th) number in the sequence is 41909002.
 
;See also:
* &nbsp; OEIS Sequence &nbsp;[[OEIS:A296712|A296712]] &nbsp; describes numbers whose digit sequence in base 10 have equal "rises" and "falls".
*[[OEIS:A296712]] the Oeis entry.
 
 
;Related tasks:
* &nbsp; [[Esthetic_numbers|Esthetic numbers]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F riseEqFall(=num)
‘Check whether a number belongs to sequence A296712.’
V height = 0
V d1 = num % 10
num I/= 10
L num != 0
V d2 = num % 10
height += (d1 < d2) - (d1 > d2)
d1 = d2
num I/= 10
R height == 0
 
V num = 0
F nextNum()
L
:num++
I riseEqFall(:num)
L.break
R :num
 
print(‘The first 200 numbers are:’)
L 200
print(nextNum(), end' ‘ ’)
print()
 
L 0 .< 10'000'000 - 200 - 1
nextNum()
print(‘The 10,000,000th number is: ’nextNum())</syntaxhighlight>
 
{{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|8080 Assembly}}==
 
<langsyntaxhighlight lang="8080asm">puts: equ 9 ; CP/M calls
putch: equ 2
org 100h
Line 115 ⟶ 153:
num: db '0 $'
;;; 24-bit counter to keep track of ten million
ctr: db 80h,96h,98h ; 1e7 = 989680h</langsyntaxhighlight>
 
{{out}}
Line 126 ⟶ 164:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm">puts: equ 9 ; MS-DOS print string
cpu 8086
bits 16
Line 203 ⟶ 241:
;;; Current number, stored as ASCII
db 0,0,0,0,0,0,0,0
num: db '0 $'</langsyntaxhighlight>
 
{{out}}
Line 212 ⟶ 250:
The 10,000,000th number is: 41909002 </pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Integer_Text_Io;
 
procedure Equal_Rise_Fall is
use Ada.Text_Io;
 
function Has_Equal_Rise_Fall (Value : Natural) return Boolean is
Rises : Natural := 0;
Falls : Natural := 0;
Image : constant String := Natural'Image (Value);
Last : Character := Image (Image'First + 1);
begin
for Pos in Image'First + 2 .. Image'Last loop
if Image (Pos) > Last then
Rises := Rises + 1;
elsif Image (Pos) < Last then
Falls := Falls + 1;
end if;
Last := Image (Pos);
end loop;
return Rises = Falls;
end Has_Equal_Rise_Fall;
 
Value : Natural := 1;
Count : Natural := 0;
begin
loop
if Has_Equal_Rise_Fall (Value) then
Count := Count + 1;
if Count <= 200 then
Ada.Integer_Text_Io.Put (Value, Width => 5);
if Count mod 20 = 0 then
New_Line;
end if;
end if;
if Count = 10_000_000 then
New_Line;
Put_Line ("The 10_000_000th: " & Natural'Image (Value));
exit;
end if;
end if;
Value := Value + 1;
end loop;
end Equal_Rise_Fall;</syntaxhighlight>
{{out}}
<pre>
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: 41909002</pre>
 
=={{header|ALGOL 68}}==
{{trans|Wren}}... with a single counter for rises and falls.
<langsyntaxhighlight 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) #
Line 252 ⟶ 349:
OD
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 273 ⟶ 370:
{{works with|Dyalog APL}}
 
<langsyntaxhighlight APLlang="apl">risefall←{
⍝ Determine if a number is in the sequence
inSeq←0=(+/2(<->)/10(⊥⍣¯1)⊢)
Line 288 ⟶ 385:
⍞←'The 10,000,000th number is: '
⎕←1e7{⍺=0:⍵-1 ⋄ (⍺-inSeq ⍵)∇ ⍵+1}1
}</langsyntaxhighlight>
 
{{out}}
Line 312 ⟶ 409:
41909002</pre>
 
=={{header|CAutoHotkey}}==
<syntaxhighlight lang="autohotkey">limit1 := 200, limit2 := 10000000
count := 0, result1 := result1 := ""
loop{
num := A_Index
if !Rise_Fall(num)
continue
count++
if (count <= limit1)
result1 .= num . (Mod(count, 20) ? "`t" : "`n")
if (count = limit2){
result2 := num
break
}
if !mod(count, 10000)
ToolTip % count
}
ToolTip
MsgBox % "The first " limit1 " numbers in the sequence:`n" result1 "`nThe " limit2 " number in the sequence is: " result2
return
 
Rise_Fall(num){
<lang C>#include <stdio.h>
rise := fall := 0
for i, n in StrSplit(num){
if (i=1)
prev := n
else if (n > prev)
rise++
else if (n < prev)
fall++
if (rise > (StrLen(num)-1) /2) || (fall > (StrLen(num)-1) /2)
return 0
prev := n
}
if (fall = rise)
return 1
}</syntaxhighlight>
{{out}}
<pre>The first 200 numbers in the sequence:
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 number in the sequence is: 41909002</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WITH_EQUAL_RISES_AND_FALLS.AWK
# converted from Go
BEGIN {
print("1-200:")
while (1) {
if (rises_equals_falls(++n)) {
if (++count <= 200) {
printf("%4d",n)
if (count % 20 == 0) {
printf("\n")
}
}
if (count == 1E7) {
printf("\n%d: %d",count,n)
break
}
}
}
exit(0)
}
function rises_equals_falls(n, d,falls,prev,rises) {
if (n < 10) {
return(1)
}
prev = -1
while (n > 0) {
d = n % 10
if (prev >= 0) {
if (d < prev) {
rises++
}
else if (d > prev) {
falls++
}
}
prev = d
n = int(n / 10)
}
return(rises == falls)
}
</syntaxhighlight>
{{out}}
<pre>
1-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
 
10000000: 41909002
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">c = 0
i = 1
while c < 10000001
if eqrf(i) then
c += 1
if c <= 200 then print i;" ";
if c = 10000000 then print : print i
end if
i += 1
end while
end
 
function eqrf(n)
sn$ = string(n)
q = 0
for i = 2 to length(sn$)
if asc(mid(sn$,i,1)) > asc(mid(sn$,i-1,1)) then
q += 1
else
if asc(mid(sn$,i,1)) < asc(mid(sn$,i-1,1)) then
q -= 1
end if
end if
next i
if q = 0 then return true else return false
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">function eqrf( n as uinteger ) as boolean
dim as string sn = str(n)
dim as integer q = 0
for i as uinteger = 2 to len(sn)
if asc(mid(sn,i,1)) > asc(mid(sn,i-1,1)) then
q += 1
elseif asc(mid(sn,i,1)) < asc(mid(sn,i-1,1)) then
q -= 1
end if
next i
if q = 0 then return true else return false
end function
 
dim as uinteger c = 0, i = 1
while c < 10000001
if eqrf(i) then
c += 1
if c <= 200 then print i;" ";
if c = 10000000 then print : print i
end if
i += 1
wend</syntaxhighlight>
{{out}}
<pre>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
41909002</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
 
Dim c As Integer = 0, i As Integer = 1
While c < 10000001
If eqrf(i) Then
c += 1
If c <= 200 Then Print " "; i;
If c = 10000000 Then Print Chr(10); i
End If
i += 1
Wend
 
End
 
Function eqrf(n As Integer) As Boolean
 
Dim sn As String = Str(n)
Dim q As Integer = 0, i As Integer
 
For i = 2 To Len(sn)
If Asc(Mid(sn, i, 1)) > Asc(Mid(sn, i - 1, 1)) Then
q += 1
Else If Asc(Mid(sn, i, 1)) < Asc(Mid(sn, i - 1, 1)) Then
q -= 1
End If
Next
If q = 0 Then Return True Else Return False
 
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="PureBasic">Procedure.b eqrf(n.i)
sn.s = Str(n)
q.i = 0
For i.i = 2 To Len(sn)
If Asc(Mid(sn, i, 1)) > Asc(Mid(sn, i - 1, 1)):
q + 1
Else
If Asc(Mid(sn, i, 1)) < Asc(Mid(sn, i - 1, 1)):
q - 1
EndIf
EndIf
Next
If q = 0:
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
 
OpenConsole()
c.i = 0
i.i = 1
While c < 10000001
If eqrf(i):
c + 1
If c <= 200:
Print(" " + Str(i))
EndIf
If c = 10000000:
PrintN(#CRLF$ + Str(i))
EndIf
EndIf
i + 1
Wend
 
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
 
/* Check whether a number has an equal amount of rises
Line 352 ⟶ 696:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 360 ⟶ 704:
 
The 10,000,000th number is: 41909002</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
bool equal_rises_and_falls(int n) {
int total = 0;
for (int previous_digit = -1; n > 0; n /= 10) {
int digit = n % 10;
if (previous_digit > digit)
++total;
else if (previous_digit >= 0 && previous_digit < digit)
--total;
previous_digit = digit;
}
return total == 0;
}
 
int main() {
const int limit1 = 200;
const int limit2 = 10000000;
int n = 0;
std::cout << "The first " << limit1 << " numbers in the sequence are:\n";
for (int count = 0; count < limit2; ) {
if (equal_rises_and_falls(++n)) {
++count;
if (count <= limit1)
std::cout << std::setw(3) << n << (count % 20 == 0 ? '\n' : ' ');
}
}
std::cout << "\nThe " << limit2 << "th number in the sequence is " << n << ".\n";
}</syntaxhighlight>
 
{{out}}
<pre>
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 10000000th number in the sequence is 41909002.
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Find how many rises and falls a number has
rises_falls = proc (n: int) returns (int,int)
rises: int := 0
falls: int := 0
while n >= 10 do
dl: int := n//10
n := n / 10
dh: int := n//10
if dh < dl then rises := rises + 1
elseif dl < dh then falls := falls + 1
end
end
return (rises, falls)
end rises_falls
 
% Generate all numbers with equal rises and falls
equal_rises_falls = iter () yields (int)
n: int := 1
rises, falls: int
while true do
rises, falls := rises_falls(n)
if rises = falls then yield (n) end
n := n + 1
end
end equal_rises_falls
 
% Show the first 200 and the 10,000,000th
start_up = proc ()
po: stream := stream$primary_output()
count: int := 0
for n: int in equal_rises_falls() do
count := count + 1
if count <= 200 then
stream$putright(po, int$unparse(n), 5)
if count//10 = 0 then stream$putc(po, '\n') end
elseif count = 10000000 then
stream$putl(po, "\nThe 10,000,000th number is: "
|| int$unparse(n))
break
end
end
end start_up</syntaxhighlight>
{{out}}
<pre> 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|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
# return the change in height of a number
sub height(n: uint32): (h: int8) is
h := 0;
var dgt := (n % 10) as uint8;
var prev: uint8;
n := n / 10;
while n > 0 loop
prev := dgt;
dgt := (n % 10) as uint8;
n := n / 10;
if prev < dgt then
h := h + 1;
elseif prev > dgt then
h := h - 1;
end if;
end loop;
end sub;
 
var number: uint32 := 0;
var seen: uint32 := 0;
var col: uint8 := 10;
 
print("The first 200 numbers are:");
print_nl();
while seen < 10000000 loop
loop
number := number + 1;
if height(number) == 0 then break; end if;
end loop;
seen := seen + 1;
if seen <= 200 then
print_i32(number);
col := col - 1;
if col != 0 then
print_char('\t');
else
print_char('\n');
col := 10;
end if;
end if;
end loop;
 
print_nl();
print("The 10,000,000th number is: ");
print_i32(number);
print_nl();</syntaxhighlight>
 
{{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|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
 
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
var T: integer;
begin
SetLength(IA,0);
repeat
begin
T:=N mod 10;
N:=N div 10;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=T;
end
until N<1;
end;
 
 
function HasEqualRiseFall(N: integer): boolean;
{Count rises and falls in numbers left to Right}
var I: integer;
var IA: TIntegerDynArray;
var Rise,Fall: integer;
begin
Rise:=0; Fall:=0;
GetDigits(N,IA);
for I:=High(IA) downto 1 do
if IA[I-1]>IA[I] then Inc(Rise)
else if IA[I-1]<IA[I] then Inc(Fall);
Result:=Rise=Fall;
end;
 
 
procedure ShowEqualRiseFall(Memo: TMemo);
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=1 to High(integer) do
if HasEqualRiseFall(I) then
begin
Inc(Cnt);
S:=S+Format('%4.0d', [I]);
if (Cnt mod 20)=0 then S:=S+CRLF;
if Cnt=200 then break;
end;
Memo.Text:=S;
Memo.Lines.Add('Count = '+IntToStr(Cnt));
 
for I:=1 to High(integer) do
if HasEqualRiseFall(I) then
begin
Inc(Cnt);
if Cnt>=10000000 then
begin
Memo.Lines.Add('10-Million: '+IntToStr(I));
break;
end;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
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
Count = 200
10-Million: 41907794
Elapsed Time: 28.140 Sec.
</pre>
 
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
fastfunc risefall n .
if n < 10
return 1
.
prev = -1
while n > 0
d = n mod 10
if prev >= 0
if d < prev
rises += 1
elif d > prev
falls += 1
.
.
prev = d
n = n div 10
.
if rises = falls
return 1
.
return 0
.
numfmt 0 4
n = 1
repeat
if risefall n = 1
cnt += 1
if cnt <= 200
write n
if cnt mod 10 = 0
print ""
.
.
.
until cnt = 1e7
n += 1
.
print ""
print n
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight 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)
Line 368 ⟶ 1,041:
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))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 380 ⟶ 1,053:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: grouping io kernel lists lists.lazy math math.extras
prettyprint tools.memory.private ;
 
Line 395 ⟶ 1,068:
 
"The 10 millionth number in OEIS:A296712 is " write
OEIS:A296712 9,999,999 [ cdr ] timesOEIS:A296712 carlnth commas print</langsyntaxhighlight>
{{out}}
<pre>
Line 415 ⟶ 1,088:
=={{header|Forth}}==
 
<langsyntaxhighlight lang="forth">: in-seq? ( n -- is N in the sequence? )
0 swap \ height
10 /mod \ digit and rest of number
Line 450 ⟶ 1,123:
." The first 200 numbers are: " two-hundred cr cr
." The 10,000,000th number is: " ten-million . cr
bye</langsyntaxhighlight>
 
{{out}}
Line 461 ⟶ 1,134:
=={{header|Fortran}}==
 
<langsyntaxhighlight Fortranlang="fortran"> PROGRAM A296712
INTEGER IDX, NUM, I
* Index and number start out at zero
Line 512 ⟶ 1,185:
IN SEQ = HEIGHT.EQ.0
RETURN
END </langsyntaxhighlight>
 
{{out}}
Line 533 ⟶ 1,206:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 579 ⟶ 1,252:
n++
}
}</langsyntaxhighlight>
 
{{out}}
Line 600 ⟶ 1,273:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.Char
 
pairs :: [a] -> [(a,a)]
Line 621 ⟶ 1,294:
putStr "The 10,000,000th number is: "
putStrLn $ show $ a296712 !! 9999999
</syntaxhighlight>
</lang>
 
{{out}}
Line 630 ⟶ 1,303:
The 10,000,000th number is: 41909002
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">NB. This would have been shorter but memory constraints forced me to test candidates
NB. in batches rather than all at once (iPads are amazing but not supercomputers...)
 
gennumberblocks =: 3 : 0
NB. y Block index. Each block is at most 100000 numbers
NB. Return one or more boxed blocks of numbers, segregated by digit count
(<. 10 ^. n) </. n =. (y * 100000) + >: i.100000
)
 
testblock =: 3 : 0
NB. y A block of numbers all with the same digit count
NB. Return those that pass the test
y #~ ((+/"1) 2 </\"1 f) = (+/"1) 2 >/\"1 "."1"0 ": ,. y
)
 
pow =: 3 : 0
NB. Generate and test one or more blocks of numbers and add the results to the
NB. running list of answers
'nextblockindex answers' =. y
(>: nextblockindex) ; answers , ; testblock &. > gennumberblocks nextblockindex
)
 
go =: 3 : 0
result =: pow ^: ((1e7&>)@#@(1&{::)) ^:_ (0 ; '')
'The first 200 numbers are:' (1!:2) 2
(10 20 $ 200 {. 1 {:: result) (1!:2) 2
'The 10,000,000th number is: ' , ": 9999999 { 1 {:: result
)</syntaxhighlight>
 
{{out}}
 
<pre>go ''
 
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|Java}}==
<syntaxhighlight lang="java">public class EqualRisesFalls {
public static void main(String[] args) {
final int limit1 = 200;
final int limit2 = 10000000;
System.out.printf("The first %d numbers in the sequence are:\n", limit1);
int n = 0;
for (int count = 0; count < limit2; ) {
if (equalRisesAndFalls(++n)) {
++count;
if (count <= limit1)
System.out.printf("%3d%c", n, count % 20 == 0 ? '\n' : ' ');
}
}
System.out.printf("\nThe %dth number in the sequence is %d.\n", limit2, n);
}
 
private static boolean equalRisesAndFalls(int n) {
int total = 0;
for (int previousDigit = -1; n > 0; n /= 10) {
int digit = n % 10;
if (previousDigit > digit)
++total;
else if (previousDigit >= 0 && previousDigit < digit)
--total;
previousDigit = digit;
}
return total == 0;
}
}</syntaxhighlight>
 
{{out}}
<pre>
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 10000000th number in the sequence is 41909002.
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq''' (*)
 
(*) gojq requires a very large amount of memory for computing the 10 millionth number in the sequence.
 
<syntaxhighlight lang="jq">def risesEqualsFalls:
. as $n
| if . < 10 then true
else {rises: 0, falls: 0, prev: -1, n: $n}
| until (.n <= 0;
(.n % 10 ) as $d
| if .prev >= 0
then if $d < .prev then .rises += 1
elif $d > .prev then .falls += 1
else .
end
else .
end
| .prev = $d
| .n = ((.n/10)|floor) )
| .rises == .falls
end ;
 
def A296712: range(1; infinite) | select(risesEqualsFalls);
 
# Override jq's incorrect definition of nth/2
# Emit the $n-th value of the stream, counting from 0; or emit nothing
def nth($n; s):
if $n < 0 then error("nth/2 doesn't support negative indices")
else label $out
| foreach s as $x (-1; .+1; select(. >= $n) | $x, break $out)
end;
 
# The tasks
"First 200:",
[limit(200; A296712)],
 
"\nThe 10 millionth number in the sequence is \(
nth(1e7 - 1; A296712))"</syntaxhighlight>
{{out}}
<pre>
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 millionth number in the sequence is 41909002
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lazy
 
function rises_and_falls(n)
Line 667 ⟶ 1,484:
 
genA296712(200, 10_000_000)
</langsyntaxhighlight>{{out}}
<pre>
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102
Line 685 ⟶ 1,502:
=={{header|MAD}}==
 
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $I8,1H:,I9*$
Line 712 ⟶ 1,529:
PRINT FORMAT FMT, I, J
END OF PROGRAM</langsyntaxhighlight>
 
{{out}}
Line 917 ⟶ 1,734:
200: 404
10000000: 41909002</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[EqualRisesAndFallsQ]
EqualRisesAndFallsQ[n_Integer] := Total[Sign[Differences[IntegerDigits[n]]]] == 0
Take[Select[Range[1000], EqualRisesAndFallsQ], 200]
valid = 0;
Dynamic[{i, valid}]
Do[
If[EqualRisesAndFallsQ[i],
valid += 1;
If[valid == 10^7, Print[i]; Break[]]
]
,
{i, 50 10^6}
]</syntaxhighlight>
{{out}}
<pre>{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}
41909002</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strutils
 
func insequence(n: Positive): bool =
## Return true if "n" is in the sequence.
if n < 10: return true
var diff = 0
var prev = n mod 10
var n = n div 10
while n != 0:
let digit = n mod 10
if digit < prev: inc diff
elif digit > prev: dec diff
prev = digit
n = n div 10
result = diff == 0
 
iterator a297712(): (int, int) =
## Yield the positions and the numbers of the sequence.
var n = 1
var pos = 0
while true:
if n.insequence:
inc pos
yield (pos, n)
inc n
 
echo "First 200 numbers in the sequence:"
for (pos, n) in a297712():
if pos <= 200:
stdout.write ($n).align(3), if pos mod 20 == 0: '\n' else: ' '
elif pos == 10_000_000:
echo "\nTen millionth number in the sequence: ", n
break</syntaxhighlight>
 
{{out}}
<pre>First 200 numbers in the sequence:
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
 
Ten millionth number in the sequence: 41909002</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
use warnings;
 
sub rf
{
local $_ = shift;
my $sum = 0;
$sum += $1 <=> $2 while /(.)(?=(.))/g;
$sum
}
 
my $count = 0;
my $n = 0;
my @numbers;
while( $count < 200 )
{
rf(++$n) or $count++, push @numbers, $n;
}
print "first 200: @numbers\n" =~ s/.{1,70}\K\s/\n/gr;
 
$count = 0;
$n = 0;
while( $count < 10e6 )
{
rf(++$n) or $count++;
}
print "\n10,000,000th number: $n\n";</syntaxhighlight>
{{out}}
<pre>
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
 
10,000,000th number: 41909002
</pre>
 
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>atom t1 = time()+1
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer count = 0, n = 0
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
printf(1,"The first 200 numbers are:\n")
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
while true do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 200 numbers are:\n"</span><span style="color: #0000FF;">)</span>
n += 1
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
integer rmf = 0,
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
l = remainder(n,10),
<span style="color: #004080;">integer</span> <span style="color: #000000;">rmf</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
r = floor(n/10)
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span>
while r do
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
integer p = remainder(r,10)
<span style="color: #008080;">while</span> <span style="color: #000000;">r</span> <span style="color: #008080;">do</span>
rmf += compare(l,p)
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
l = p
<span style="color: #000000;">rmf</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
r = floor(r/10)
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
end while
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
if rmf=0 then
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">rmf</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if count<=200 then
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"%3d ",n)
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">200</span> <span style="color: #008080;">then</span>
if remainder(count,20)=0 then
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
printf(1,"\n")
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if count == 1e7 then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
progress("")
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">1e7</span> <span style="color: #008080;">then</span>
printf(1,"\nThe %,dth number is %,d\n",{count,n})
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
exit
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe %,dth number is %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #008080;">exit</span>
if time()>t1 then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
progress("%,d:%,d\r",{count,n})
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
t1 = time()+1
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%,d:%,d\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 971 ⟶ 1,908:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">import itertools
 
def riseEqFall(num):
Line 1,004 ⟶ 1,941:
# It is necessary to subtract 200 from the index, because 200 numbers
# have already been consumed.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,013 ⟶ 1,950:
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ stack ] is rises
[ stack ] is falls
 
[ 0 rises put
0 falls put
digits
behead swap witheach
[ tuck 2dup < iff
[ 1 rises tally
2drop ] done
> if
[ 1 falls tally ] ]
drop
rises take
falls take = ] is equal ( n --> b )
 
[] 0
[ 1+ dup equal if
[ tuck join swap ]
over size 200 = until ]
drop
echo
cr cr
0 0
[ 1+ dup equal if
[ dip 1+ ]
over 10000000 = until ]
nip
echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 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 ]
 
41909002</pre>
 
=={{header|Raku}}==
{{works with|Rakudo|2020.09}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use Base::Any;
 
Line 1,072 ⟶ 2,052:
put "Base {$base.fmt(<%2d>)} (https://oeis.org/A$oeis): ",
$upto.map({rf(+$base, Any, &infix:«<»)[.exp(10) - 1].&to-base($base)}).join: ', '
}</langsyntaxhighlight>
{{out}}
<pre>Rise = Fall:
Line 1,149 ⟶ 2,129:
To do the heavy lifting, &nbsp; this REXX program constructs a table of every two-digit sequence which indicates a
<br>'''rise''' &nbsp; (+1), &nbsp; &nbsp; '''fall''' &nbsp; (-1), &nbsp; &nbsp; or &nbsp; '''neither''' &nbsp; (0).
<langsyntaxhighlight 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.*/
Line 1,155 ⟶ 2,135:
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 /*test integers until we have N of them*/
s= 0 /*initialize the sum of rises/falls. */
do k=1 for Lm; t= substrlength(j,k,2)-1 /*obtain a set of two digs from number.*/
s= s + @.t t= substr(j, k, 2) /*sumobtain thea risesset andof fallstwo indigs thefrom number.*/
end s= s + @.t /*ksum 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($j) /*show Nth #.*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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))
/*──────────────────────────────────────────────────────────────────────────────────────*/
init: @.= 0; do i=1 for 9; _= i' '; @._= 1; _= '0'i; @._= +1; end /*i*/
Line 1,181 ⟶ 2,163:
end /*o*/ /* [↑] display 20 numbers to a line.*/
 
if _\=='' then say substr(_, 2); return /*handle any residual numbers in list. */</syntaxhighlight>
/*──────────────────────────────────────────────────────────────────────────────────────*/
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>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,203 ⟶ 2,182:
<pre>
the 10,000,000th number is: 41,909,002
</pre>
 
=={{header|RPL}}==
≪ '''IF''' DUP 9 ≤ '''THEN''' SIGN
'''ELSE''' →STR → num
≪ 0 num 1 1 SUB NUM
2 num SIZE '''FOR''' j
num j DUP SUB NUM SWAP OVER - SIGN ROT + SWAP '''NEXT'''
DROP NOT
≫ '''END'''
≫ ''''RNF'''' STO ''( n -- boolean )''
≪ → test n
≪ { } 1 9 CF '''WHILE''' 9 FC? '''REPEAT'''
'''IF''' DUP test EVAL '''THEN''' SWAP OVER + SWAP '''IF''' OVER SIZE n ≥ '''THEN''' 9 SF '''END END'''
1 +
'''END''' DROP
≫ ≫ ''''TBOOL'''' STO ''( 'FUNC' n -- { first_n_true_integers } )''
 
'RNF' 200 '''TBOOL'''
{{out}}
<pre>
1: { 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 }
</pre>
Due to emulator’s watchdog timer, the ten millionth number is out of reach.
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">class Integer
def eq_rise_fall? = digits.each_cons(2).sum{|a,b| a <=> b} == 0
end
 
puts (1..).lazy.select(&:eq_rise_fall?).take(200).force.join(" ")
 
n = 10_000_000
res = (1..).lazy.select(&:eq_rise_fall?).take(n).drop(n-1).first
puts "The #{n}th number in the sequence is #{res}."
</syntaxhighlight>
{{out}}
<pre>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 10000000th number in the sequence is 41909002.
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func isok(arr) {
var diffs = arr.map_cons(2, {|a,b| a - b })
diffs.count { .is_pos } == diffs.count { .is_neg }
}
 
var base = 10
 
with (200) {|n|
say "First #{n} terms (base #{base}):"
n.by { isok(.digits(base)) && .is_pos }.each_slice(20, {|*a|
say a.map { '%3s' % _ }.join(' ')
})
}
 
with (1e7) {|n| # takes a very long time
say "\nThe #{n.commify}-th term (base #{base}): #{
n.th { isok(.digits(base)) && .is_pos }.commify}"
}</syntaxhighlight>
{{out}}
<pre>
First 200 terms (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,000-th term (base 10): 41,909,002
</pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func equalRisesAndFalls(_ n: Int) -> Bool {
var total = 0
var previousDigit = -1
var m = n
while m > 0 {
let digit = m % 10
m /= 10
if previousDigit > digit {
total += 1
} else if previousDigit >= 0 && previousDigit < digit {
total -= 1
}
previousDigit = digit
}
return total == 0
}
 
var count = 0
var n = 0
let limit1 = 200
let limit2 = 10000000
print("The first \(limit1) numbers in the sequence are:")
while count < limit2 {
n += 1
if equalRisesAndFalls(n) {
count += 1
if count <= limit1 {
print(String(format: "%3d", n), terminator: count % 20 == 0 ? "\n" : " ")
}
}
}
print("\nThe \(limit2)th number in the sequence is \(n).")</syntaxhighlight>
 
{{out}}
<pre>
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 10000000th number in the sequence is 41909002.
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var risesEqualsFalls = Fn.new { |n|
Line 1,245 ⟶ 2,352:
}
n = n + 1
}</langsyntaxhighlight>
 
{{out}}
Line 1,262 ⟶ 2,369:
 
The 10 millionth number in the sequence is 41,909,002.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func RiseFall(N); \Return 'true' if N has equal rises and falls
int N, R, F, D, D0;
[R:= 0; F:= 0;
N:= N/10;
D0:= rem(0);
while N do
[N:= N/10;
D:= rem(0);
if D > D0 then R:= R+1;
if D < D0 then F:= F+1;
D0:= D;
];
return R = F;
];
 
int N, Cnt;
[N:= 1;
Cnt:= 0;
loop [if RiseFall(N) then
[Cnt:= Cnt+1;
if Cnt <= 200 then
[IntOut(0, N);
if rem (Cnt/10) = 0 then CrLf(0)
else ChOut(0, 9\tab\);
];
if Cnt = 10_000_000 then
[Text(0, "10 millionth number is ");
IntOut(0, N); CrLf(0);
quit;
];
];
N:= N+1;
];
]</syntaxhighlight>
 
{{out}}
<pre>
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
10 millionth number is 41909002
</pre>
1,995

edits