Numbers with equal rises and falls: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 31:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F riseEqFall(=num)
‘Check whether a number belongs to sequence A296712.’
V height = 0
Line 58:
L 0 .< 10'000'000 - 200 - 1
nextNum()
print(‘The 10,000,000th number is: ’nextNum())</langsyntaxhighlight>
 
{{out}}
Line 69:
=={{header|8080 Assembly}}==
 
<langsyntaxhighlight lang="8080asm">puts: equ 9 ; CP/M calls
putch: equ 2
org 100h
Line 153:
num: db '0 $'
;;; 24-bit counter to keep track of ten million
ctr: db 80h,96h,98h ; 1e7 = 989680h</langsyntaxhighlight>
 
{{out}}
Line 164:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm">puts: equ 9 ; MS-DOS print string
cpu 8086
bits 16
Line 241:
;;; Current number, stored as ASCII
db 0,0,0,0,0,0,0,0
num: db '0 $'</langsyntaxhighlight>
 
{{out}}
Line 251:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Integer_Text_Io;
 
Line 294:
Value := Value + 1;
end loop;
end Equal_Rise_Fall;</langsyntaxhighlight>
{{out}}
<pre>
Line 312:
=={{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 349:
OD
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 370:
{{works with|Dyalog APL}}
 
<langsyntaxhighlight APLlang="apl">risefall←{
⍝ Determine if a number is in the sequence
inSeq←0=(+/2(<->)/10(⊥⍣¯1)⊢)
Line 385:
⍞←'The 10,000,000th number is: '
⎕←1e7{⍺=0:⍵-1 ⋄ (⍺-inSeq ⍵)∇ ⍵+1}1
}</langsyntaxhighlight>
 
{{out}}
Line 410:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">limit1 := 200, limit2 := 10000000
count := 0, result1 := result1 := ""
loop{
Line 445:
if (fall = rise)
return 1
}</langsyntaxhighlight>
{{out}}
<pre>The first 200 numbers in the sequence:
Line 462:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_WITH_EQUAL_RISES_AND_FALLS.AWK
# converted from Go
Line 503:
return(rises == falls)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 523:
=={{header|C}}==
 
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
/* Check whether a number has an equal amount of rises
Line 561:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 571:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 600:
}
std::cout << "\nThe " << limit2 << "th number in the sequence is " << n << ".\n";
}</langsyntaxhighlight>
 
{{out}}
Line 620:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Find how many rises and falls a number has
rises_falls = proc (n: int) returns (int,int)
rises: int := 0
Line 663:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 11
Line 689:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# return the change in height of a number
Line 737:
print("The 10,000,000th number is: ");
print_i32(number);
print_nl();</langsyntaxhighlight>
 
{{out}}
Line 767:
 
=={{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 773:
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 785:
=={{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 800:
 
"The 10 millionth number in OEIS:A296712 is " write
9,999,999 OEIS:A296712 lnth commas print</langsyntaxhighlight>
{{out}}
<pre>
Line 820:
=={{header|Forth}}==
 
<langsyntaxhighlight lang="forth">: in-seq? ( n -- is N in the sequence? )
0 swap \ height
10 /mod \ digit and rest of number
Line 855:
." The first 200 numbers are: " two-hundred cr cr
." The 10,000,000th number is: " ten-million . cr
bye</langsyntaxhighlight>
 
{{out}}
Line 866:
=={{header|Fortran}}==
 
<langsyntaxhighlight Fortranlang="fortran"> PROGRAM A296712
INTEGER IDX, NUM, I
* Index and number start out at zero
Line 917:
IN SEQ = HEIGHT.EQ.0
RETURN
END </langsyntaxhighlight>
 
{{out}}
Line 937:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function eqrf( n as uinteger ) as boolean
dim as string sn = str(n)
dim as integer q = 0
Line 958:
end if
i += 1
wend</langsyntaxhighlight>
{{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
Line 965:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,011:
n++
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,032:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.Char
 
pairs :: [a] -> [(a,a)]
Line 1,053:
putStr "The 10,000,000th number is: "
putStrLn $ show $ a296712 !! 9999999
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,064:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="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...)
 
Line 1,091:
(10 20 $ 200 {. 1 {:: result) (1!:2) 2
'The 10,000,000th number is: ' , ": 9999999 { 1 {:: result
)</langsyntaxhighlight>
 
{{out}}
Line 1,111:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class EqualRisesFalls {
public static void main(String[] args) {
final int limit1 = 200;
Line 1,139:
return total == 0;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,164:
(*) gojq requires a very large amount of memory for computing the 10 millionth number in the sequence.
 
<langsyntaxhighlight lang="jq">def risesEqualsFalls:
. as $n
| if . < 10 then true
Line 1,197:
 
"\nThe 10 millionth number in the sequence is \(
nth(1e7 - 1; A296712))"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,208:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lazy
 
function rises_and_falls(n)
Line 1,243:
 
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 1,261:
=={{header|MAD}}==
 
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
VECTOR VALUES FMT = $I8,1H:,I9*$
Line 1,288:
PRINT FORMAT FMT, I, J
END OF PROGRAM</langsyntaxhighlight>
 
{{out}}
Line 1,495:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[EqualRisesAndFallsQ]
EqualRisesAndFallsQ[n_Integer] := Total[Sign[Differences[IntegerDigits[n]]]] == 0
Take[Select[Range[1000], EqualRisesAndFallsQ], 200]
Line 1,507:
,
{i, 50 10^6}
]</langsyntaxhighlight>
{{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}
Line 1,513:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
func insequence(n: Positive): bool =
Line 1,545:
elif pos == 10_000_000:
echo "\nTen millionth number in the sequence: ", n
break</langsyntaxhighlight>
 
{{out}}
Line 1,563:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,591:
rf(++$n) or $count++;
}
print "\n10,000,000th number: $n\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 1,612:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 1,647:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,667:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">import itertools
 
def riseEqFall(num):
Line 1,700:
# It is necessary to subtract 200 from the index, because 200 numbers
# have already been consumed.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,713:
{{works with|Rakudo|2020.09}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use Base::Any;
 
Line 1,768:
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,845:
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,879:
end /*o*/ /* [↑] display 20 numbers to a line.*/
 
if _\=='' then say substr(_, 2); return /*handle any residual numbers in list. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,901:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Integer
def eq_rise_fall? = digits.each_cons(2).sum{|a,b| a <=> b} == 0
end
Line 1,910:
res = (1..).lazy.select(&:eq_rise_fall?).take(n).drop(n-1).first
puts "The #{n}th number in the sequence is #{res}."
</syntaxhighlight>
</lang>
{{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
Line 1,916:
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func isok(arr) {
var diffs = arr.map_cons(2, {|a,b| a - b })
diffs.count { .is_pos } == diffs.count { .is_neg }
Line 1,933:
say "\nThe #{n.commify}-th term (base #{base}): #{
n.th { isok(.digits(base)) && .is_pos }.commify}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,952:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func equalRisesAndFalls(_ n: Int) -> Bool {
Line 1,985:
}
}
print("\nThe \(limit2)th number in the sequence is \(n).")</langsyntaxhighlight>
 
{{out}}
Line 2,006:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var risesEqualsFalls = Fn.new { |n|
Line 2,044:
}
n = n + 1
}</langsyntaxhighlight>
 
{{out}}
Line 2,064:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func RiseFall(N); \Return 'true' if N has equal rises and falls
int N, R, F, D, D0;
[R:= 0; F:= 0;
Line 2,097:
N:= N+1;
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits