Count in octal: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) |
|||
(33 intermediate revisions by 17 users not shown) | |||
Line 14:
=={{header|0815}}==
<
}:o: Treat the queue as a stack and
<:8:= accumulate the octal digits
Line 30:
<:a:~$ Output a newline.
<:1:x{+ Dequeue the current number and increment it.
^:l:</
=={{header|360 Assembly}}==
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible.
<
OCTAL CSECT
USING OCTAL,R13 base register
Line 79:
EM12 DC X'40',9X'20',X'2120' mask CL12 11num
YREGS
END OCTAL</
{{out}}
<pre style="height:20ex">
Line 108:
{{works with|https://skilldrick.github.io/easy6502/ Easy6502}}
Easy6502 can only output using a limited video memory or a hexdump. However the output is correct up to 2 octal digits.
<
define SRC_LO $00
define SRC_HI $01
Line 168:
ora temp ;put the bottom 3 bits of the original input back.
and #$7F ;clear bit 7.
rts</
{{out}}
Line 180:
=={{header|8080 Assembly}}==
This assumes the CP/M operating system. The count will terminate after the largest unsigned 16-bit value is reached.
<syntaxhighlight lang="text">
;-------------------------------------------------------
; useful equates
Line 236:
ret
;-------------------------------------------------------
; logical right shift of 16-bit value in HL
;-------------------------------------------------------
shlr: ora a ; clear carry
Line 276:
;
end
</syntaxhighlight>
{{out}}
Showing the last 10 lines of the output.
Line 294:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program countOctal64.s */
Line 395:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
CHAR ARRAY a(6)
BYTE i=[0]
Line 424:
UNTIL i=0
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_in_octal.png Screenshot from Atari 8-bit computer]
Line 443:
=={{header|Ada}}==
<
procedure Octal is
Line 452:
Ada.Text_IO.New_Line;
end loop;
end Octal;</
First few lines of Output:
<pre> 8#0#
Line 473:
=={{header|Aime}}==
<
o = 0;
Line 480:
o_byte('\n');
o += 1;
} while (0 < o);</
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<
INT oct width = (bits width-1) OVER 3 + 1;
Line 493:
printf(($"8r"8r n(oct width)dl$, BIN i))
OD
)</
Output:
<pre>
Line 513:
8r00000000020
8r00000000021
</pre>
=={{header|ALGOL-M}}==
<syntaxhighlight lang = "ALGOL">
begin
% display n on console in octal format %
procedure putoct(n);
integer n;
begin
integer digit, n8;
string(1) array octdig[0:7];
octdig[0] := "0"; octdig[1] := "1"; octdig[2] := "2";
octdig[3] := "3"; octdig[4] := "4"; octdig[5] := "5";
octdig[6] := "6"; octdig[7] := "7";
n8 := n / 8;
if n8 <> 0 then putoct(n8); % recursive call %
digit := n - (n / 8) * 8; % n mod 8 %
writeon(octdig[digit]);
end;
integer i, maxint;
i := 1;
maxint := 16383;
comment
Excercise the procedure by counting up in octal as
far as possible. In doing so, we have to take some
care, because integer variables are set to 1 on
overflow, and if that happens, the loop will simply
start over, and the program will run forever;
while i < maxint do % we need to stop one shy %
begin
write("");
putoct(i);
i := i + 1;
end;
% display the final value %
write("");
putoct(maxint);
end
</syntaxhighlight>
{{out}}
First and last 10 lines of output
<pre>
1
2
3
4
5
6
7
10
11
12
...
37766
37767
37770
37771
37772
37773
37774
37775
37776
37777
</pre>
=={{header|ALGOL W}}==
Algol W has built-in hexadecimal and decimal output, this implements octal output.
<
string(12) r;
string(8) octDigits;
Line 543 ⟶ 612:
for c := cPos - 1 step -1 until 0 do writeon( r( c // 1 ) )
end while_r_lt_MAXINTEGER
end.</
{{out}}
<pre>
Line 558 ⟶ 627:
12
...
</pre>
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
algoritmo
x=0, tope=11
decimales '0'
iterar grupo ( ++x,#( x< tope ),\
x,":",justificar derecha ( 5, x ---mostrar como octal--- ),\
NL, imprimir, cuando( #(x==10)){ \
"...\n...\n",x=4294967284, tope=4294967295} )
terminar
</syntaxhighlight>
{{out}}
<pre>
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 10
9: 11
10: 12
...
...
4294967285: 37777777765
4294967286: 37777777766
4294967287: 37777777767
4294967288: 37777777770
4294967289: 37777777771
4294967290: 37777777772
4294967291: 37777777773
4294967292: 37777777774
4294967293: 37777777775
4294967294: 37777777776
</pre>
=={{header|APL}}==
Works with [[Dyalog APL]]. 100,000 is just an arbitrarily large number I chose.
<syntaxhighlight lang
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program countoctal.s */
Line 679 ⟶ 789:
bx lr @return
</syntaxhighlight>
=={{header|Arturo}}==
<
print ["number in base 10:" pad to :string i 2
"number in octal:" pad as.octal i 2]</
{{out}}
Line 731 ⟶ 841:
=={{header|AutoHotkey}}==
<
Octal(int){
While int
Line 741 ⟶ 851:
FileAppend, % Octal(A_Index) "`n", CONOUT$
Sleep 200
}</
=={{header|AWK}}==
The awk extraction and reporting language uses the underlying C library to provide support for the printf command. This enables us to use that function to output the counter value as octal:
<
for (l = 0; l <= 2147483647; l++) {
printf("%o\n", l);
}
}</
=={{header|BASIC}}==
Line 758 ⟶ 868:
{{works with|QBasic}}
<
FOR n = 0 TO &h7FFFFFFF
PRINT OCT$(n)
NEXT</
However, many do not. For those BASICs, we need to write our own function.
<syntaxhighlight lang="qbasic">WHILE ("" = INKEY$)
PRINT Octal$(n)
n = n + 1
Line 781 ⟶ 889:
WEND
Octal$ = outp$
END FUNCTION</
See also: [[#BBC BASIC|BBC BASIC]], [[#Liberty BASIC|Liberty BASIC]], [[#PureBasic|PureBasic]], [[#Run BASIC|Run BASIC]]
==={{header|Applesoft BASIC}}===
<
100 O$ = N$
Line 798 ⟶ 906:
180 NEXT I
190 IF C THEN N$ = "1" + N$
200 GOTO 100</
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
valor = 0
do
Line 808 ⟶ 916:
until valor = 0
end
</syntaxhighlight>
=== {{header|Chipmunk Basic}} ===
<syntaxhighlight lang="basic">
10 rem Count in ocatal
20 while ("" = inkey$ )
30 print octal$(n)
40 n = n+1
50 wend
60 end
200 function octal$(what)
210 outp$ = ""
220 w = what
230 while abs(w) > 0
240 o = w and 7
250 w = int(w/8)
260 outp$ = str$(o)+outp$
270 wend
280 octal$ = outp$
290 end function
</syntaxhighlight>
==={{header|Commodore BASIC}}===
Line 819 ⟶ 946:
Commodore BASIC has a little quirk where numeric values converted to a string also include a leading space for the possible negative sign; this is why the <code>STR$</code> function is wrapped in a <code>RIGHT$</code> function.
<
20 gosub 70
30 print oc$
Line 830 ⟶ 957:
100 oc$=left$(str$(n),1)+right$(str$(r),1)+oc$
110 if q<>0 then t=q:goto 80
120 return</
{{output}}
Line 863 ⟶ 990:
==={{header|Sinclair ZX81 BASIC}}===
The octal number is stored and manipulated as a string, meaning that even with only 1k of RAM the program shouldn't stop until the number gets to a couple of hundred digits long. I have <i>not</i> left it running long enough to find out exactly when it does run out of memory. The <code>SCROLL</code> statement is necessary: the ZX81 halts when the screen is full unless it is positively told to scroll instead.
<
20 SCROLL
30 PRINT N$
Line 876 ⟶ 1,003:
120 GOTO 50
130 LET N$="1"+N$
140 GOTO 20</
==={{header|uBasic/4tH}}===
This routine allows for any base (up to 36) and also caters for negative numbers.
<syntaxhighlight lang="text">x = 1
Do
Line 892 ⟶ 1,019:
Local (2) ' digit C@ and string D@
' initialize, save sign
d@ :=
Do
Line 902 ⟶ 1,029:
If Pop() Then d@ = Join ("-", d@) ' apply sign if required
Return (d@)
</syntaxhighlight>
=={{header|Batch File}}==
<
@echo off
:: {CTRL + C} to exit the batch file
Line 931 ⟶ 1,058:
set /a todivide/=8
goto loop2
</syntaxhighlight>
{{out}}
<pre>
Line 948 ⟶ 1,075:
=={{header|BBC BASIC}}==
Terminate by pressing ESCape.
<
REPEAT
PRINT FN_tobase(N%, 8, 0)
Line 966 ⟶ 1,093:
UNTIL (N%=FALSE OR N%=TRUE) AND M%<=0
=A$
</syntaxhighlight>
=={{header|bc}}==
<
for (num = 0; 1; num++) num /* Loop forever, printing counter. */</
The loop never stops at a maximum value, because bc uses [[arbitrary-precision integers (included)|arbitrary-precision integers]].
Line 976 ⟶ 1,103:
=={{header|BCPL}}==
This will count up from 0 until the limit of the machine word.
<
let start() be
Line 984 ⟶ 1,111:
x := x + 1
$) repeatuntil x = 0
$)</
=={{header|Befunge}}==
This is almost identical to the [[Binary digits#Befunge|Binary digits]] sample, except for the change of base and the source coming from a loop rather than a single input.
<
=={{header|BQN}}==
<code>_while_</code> and <code>Oct</code> are snippets from [https://mlochbaum.github.io/bqncrate/ BQNcrate]. A more array-oriented approach is <code>⥊↕n⥊8</code>, which produces all <code>n</code>-digit octal numbers instead of counting.
<
Oct←8{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)}
{•Show Oct 𝕩, 𝕩+1} _while_ 1 0 </
=={{header|Bracmat}}==
Stops when the user presses Ctrl-C or when the stack overflows. The solution is not elegant, and so is octal counting.
<
( oct
=
Line 1,008 ⟶ 1,135:
& -1:?n
& whl'(1+!n:?n&out$(!n oct$!n));
</syntaxhighlight>
=={{header|Brainf***}}==
<
[>>++++++++<< Set up {n 0 8} for divmod magic
[->+>- Then
Line 1,028 ⟶ 1,155:
<[[-]<] Zero the tape for the next iteration
++++++++++. Print a newline
[-]<+] Zero it then increment n and go again</
=={{header|C}}==
<
int main()
Line 1,038 ⟶ 1,165:
do { printf("%o\n", i++); } while(i);
return 0;
}</
=={{header|C sharp|C#}}==
<
class Program
Line 1,053 ⟶ 1,180:
} while (++number > 0);
}
}</
=={{header|C++}}==
This prevents an infinite loop by counting until the counter overflows and produces a 0 again. This could also be done with a for or while loop, but you'd have to print 0 (or the last number) outside the loop.
<
int main()
Line 1,069 ⟶ 1,196:
} while(i != 0);
return 0;
}</
=={{header|Clojure}}==
<
=={{header|COBOL}}==
{{trans|Delphi}}
{{works with|GNU Cobol|2.0}}
<
IDENTIFICATION DIVISION.
PROGRAM-ID. count-in-octal.
Line 1,120 ⟶ 1,247:
END-PERFORM
.
END FUNCTION dec-to-oct.</
=={{header|CoffeeScript}}==
<
n = 0
Line 1,129 ⟶ 1,256:
console.log n.toString(8)
n += 1
</syntaxhighlight>
=={{header|Common Lisp}}==
<
=={{header|Component Pascal}}==
BlackBox Component Builder
<
MODULE CountOctal;
IMPORT StdLog,Strings;
Line 1,152 ⟶ 1,279:
END CountOctal.
</syntaxhighlight>
Execute: ^Q CountOctal.Do<br/>
Output:
Line 1,178 ⟶ 1,305:
=={{header|Cowgol}}==
<
typedef N is uint16;
Line 1,201 ⟶ 1,328:
n := n + 1;
if n == 0 then break; end if;
end loop;</
=={{header|Crystal}}==
<
# using unsigned 8 bit integer, range 0 to 255
(0_u8..255_u8).each { |i| puts i.to_s(8) }</
{{out}}
Line 1,230 ⟶ 1,357:
=={{header|D}}==
<
import std.stdio;
Line 1,236 ⟶ 1,363:
do writefln("%o", i++);
while(i);
}</
=={{header|Dc}}==
=== Named Macro ===
A simple infinite loop and octal output will do.
<syntaxhighlight lang
=== Anonymous Macro ===
Needs <code>r</code> (swap TOS and NOS):
<
Pushing/poping TOS to a named stack can be used instead of swaping:
<
=={{header|DCL}}==
<
$ loop:
$ write sys$output f$fao( "!OL", i )
$ i = i + 1
$ goto loop</
{{out}}
<pre>00000000000
Line 1,270 ⟶ 1,397:
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 1,293 ⟶ 1,420:
for i := 0 to 20 do
WriteLn(DecToOct(i));
end.</
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func$ oct v .
while v > 0
r$ = v mod 8 & r$
v = v div 8
.
if r$ = ""
r$ = 0
.
return r$
.
for i = 0 to 10
print oct i
.
print "."
print "."
max = pow 2 53
i = max - 10
repeat
print oct i
until i = max
i += 1
.
</syntaxhighlight>
=={{header|EDSAC order code}}==
Uses 17-bit integers, maximum 2^16 - 1 (177777 octal). It would take the original EDSAC 18 or 19 hours to exhaust these, so there is not much point in extending to 35-bit integers.
<
[Count in octal, for Rosetta Code.
EDSAC program, Initial Orders 2.]
Line 1,359 ⟶ 1,512:
PF [acc = 0 on entry]
[end]
</syntaxhighlight>
{{out}}
<pre>
Line 1,378 ⟶ 1,531:
=={{header|Elixir}}==
<
or
<
IO.puts Integer.to_string(n,8)
{n,n+1}
end) |> Stream.run</
or
<
f.(f, 0)</
=={{header|Emacs Lisp}}==
Displays in the message area interactively, or to standard output under <code>-batch</code>.
<
(message "%o" i))</
=={{header|Erlang}}==
The fun is copied from [[Integer sequence#Erlang]]. I changed the display format.
<syntaxhighlight lang="erlang">
F = fun(FF, I) -> io:fwrite("~.8B~n", [I]), FF(FF, I + 1) end.
</syntaxhighlight>
Use like this:
<pre>
Line 1,405 ⟶ 1,558:
=={{header|Euphoria}}==
<
i = 0
while 1 do
printf(1,"%o\n",i)
i += 1
end while</
Output:
Line 1,427 ⟶ 1,580:
=={{header|F Sharp|F#}}==
<
printfn "%o" num
countInOctal (num + 1)
countInOctal 1</
=={{header|Factor}}==
<
0 [ dup .o 1 + t ] loop</
=={{header|Forth}}==
Using INTS from [[Integer sequence#Forth]]
<
octal ints</
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<
implicit none
Line 1,457 ⟶ 1,610:
n = n + 1
end do
end program</
=={{header|FreeBASIC}}==
<
Dim ub As UByte = 0 ' only has a range of 0 to 255
Line 1,469 ⟶ 1,622:
Print
Print "Press any key to quit"
Sleep</
=={{header|Frink}}==
<
while true
{
println[i -> octal]
i = i + 1
}</
=={{header|Futhark}}==
Line 1,484 ⟶ 1,637:
look like octal numbers when printed in decimal.
<syntaxhighlight lang="futhark">
fun octal(x: int): int =
loop ((out,mult,x) = (0,1,x)) = while x > 0 do
Line 1,494 ⟶ 1,647:
fun main(n: int): [n]int =
map octal (iota n)
</syntaxhighlight>
=={{header|FutureBasic}}==
<
defstr word
Line 1,508 ⟶ 1,661:
next
HandleEvents</
Output:
Line 1,540 ⟶ 1,693:
=={{header|Go}}==
<
import (
Line 1,554 ⟶ 1,707:
}
}
}</
Output:
<pre>
Line 1,574 ⟶ 1,727:
</pre>
Note that to use a different integer type, code must be changed in two places. Go has no way to query a type for its maximum value. Example:
<
for i := uint16(0); ; i++ { // type specified here
fmt.Printf("%o\n", i)
Line 1,581 ⟶ 1,734:
}
}
}</
Output:
<pre>
Line 1,590 ⟶ 1,743:
</pre>
Note also that if floating point types are used for the counter, loss of precision will prevent the program from from ever reaching the maximum value. If you stretch interpretation of the task wording "maximum value" to mean "maximum value of contiguous integers" then the following will work:
<
func main() {
Line 1,606 ⟶ 1,759:
i = next
}
}</
Output, with skip uncommented:
<pre>
Line 1,620 ⟶ 1,773:
</pre>
Big integers have no maximum value, but the Go runtime will panic when memory allocation fails. The deferred recover here allows the program to terminate silently should the program run until this happens.
<
"big"
"fmt"
Line 1,633 ⟶ 1,786:
fmt.Printf("%o\n", i)
}
}</
Output:
<pre>
Line 1,654 ⟶ 1,807:
=={{header|Groovy}}==
Size-limited solution:
<
for (def i = 0; i <= Integer.MAX_VALUE; i++) {
printf ('%7d %#5o\n', i, i)
}</
Unbounded solution:
<
for (def i = 0g; true; i += 1g) {
printf ('%7d %#5o\n', i, i)
}</
Output:
Line 1,688 ⟶ 1,841:
=={{header|Haskell}}==
<
main :: IO ()
Line 1,694 ⟶ 1,847:
mapM_
(putStrLn . flip showOct "")
[1 .. maxBound :: Int]</
=={{header|Icon}} and {{header|Unicon}}==
<
procedure main()
limit := 8r37777777777
every write(exbase10(seq(0)\limit, 8))
end</
=={{header|J}}==
'''Solution:'''
<
The full result is not displayable, by design.
<
0
1
Line 1,722 ⟶ 1,875:
10
11
...</
The important part of this code is 8&#.inv which converts numbers from internal representation to a sequence of base 8 digits. (We then convert this sequence to characters
So then we define disp as a word which displays its argument in octal and returns its argument as its result (unchanged).
Finally, the <code>^:_</code> clause tells J to repeat this function forever, with <code>(1 + disp)</code>adding 1 to the result each time it is displayed (or at least that clause tells J to keep repeating that operation until it gives the same value back twice in a row - which won't happen - or to stop when the machine stops - like if the power is turned off - or if J is shut down - or...).
We use arbitrary precision numbers, not because there's any likelihood that fixed width numbers would ever overflow, but to emphasize that this thing is going to have to be shut down by some mechanism outside the program.
Line 1,734 ⟶ 1,887:
That said... note that what we are doing here is counting using an internal representation and converting that to octal for display. If we instead wanted to add 1 to a value provided to us in octal and provide the result in octal, we might instead use <code>>:</code> (add 1) and wrap it in <code>&.(8&#.)</code> (convert argument from octal and use inverse on result) with a list of digits representing our octal number. For example:
<
7 7
>:&.(8&#.)7 7
1 0 0
>:&.(8&#.)1 0 0
1 0 1</
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn main() {
for i in (0..) {
println("{:o}", i)
}
}
</syntaxhighlight>
=={{header|Janet}}==
<syntaxhighlight lang="janet">
(loop [i :range [0 math/int-max]]
(printf "%o" i))
</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang
void printCount() {
for (int value = 0; value <= 20; value++) {
/* the 'o' specifier will print the octal integer */
System.out.printf("%o%n", value);
}
}
</syntaxhighlight>
<pre>
0
1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
20
21
22
23
24
</pre>
<br />
An alternate implementation
<syntaxhighlight lang="java">public class Count{
public static void main(String[] args){
for(int i = 0;i >= 0;i++){
Line 1,748 ⟶ 1,949:
}
}
}</
=={{header|JavaScript}}==
<
document.writeln(n.toString(8)); // not sure what's the best way to output it in JavaScript
}</
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE
digit == "01234567" of;
oct == "\n" [pop 7 >] [[8 div digit] dip cons] while swap digit swons.
0 [0 >=] [dup oct putchars succ] while pop.</syntaxhighlight>
=={{header|jq}}==
Here we use JSON strings of octal digits to represent octal numbers, and therefore there is no language-defined upper bound for the problem. We are careful therefore to select an algorithm that will continue indefinitely so long as there are sufficient physical resources. This is done by framing the problem so that we can use jq's `recurse(_)`.
<
def octals:
# input and output: array of octal digits in reverse order
Line 1,774 ⟶ 1,982:
[0] | recurse(octal_add1) | reverse | join("");
octals</
To print the octal strings without quotation marks, invoke jq with the -r command-line option.
=={{header|Julia}}==
<syntaxhighlight lang="julia">
for i in one(Int64):typemax(Int64)
print(oct(i), " ")
sleep(0.1)
end
</syntaxhighlight>
I slowed the loop down with a <code>sleep</code> to make it possible to see the result without being swamped.
Line 1,792 ⟶ 2,000:
=={{header|Klingphix}}==
<
:octal "" >ps [dup 7 band tostr ps> chain >ps 8 / int] [dup abs 0 >] while ps> tonum bor ;
Line 1,798 ⟶ 2,006:
( 0 10 ) sequence @octal map pstack
" " input</
{{out}}
<pre>((0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12))</pre>
=={{header|Kotlin}}==
<
// counts up to 177 octal i.e. 127 decimal
fun main(args: Array<String>) {
(0..Byte.MAX_VALUE).forEach { println("%03o".format(it)) }
}</
{{out}}
Line 1,828 ⟶ 2,036:
LabVIEW contains a Number to Octal String function. The following image shows the front panel and block diagram.<br/>
[[file:Count_in_octal.png]]
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$i = 0
while($i >= 0) {
fn.println(fn.toTextBase($i, 8))
$i += 1
}
</syntaxhighlight>
=={{header|Lang5}}==
<
0 do dup 1 compress . "\n" . 1 + loop</
=={{header|langur}}==
We
We use the :8x interpolation modifier to create a string in base 8 (may use base 2 to 36).
<
for .i
writeln $"10x\.i; == 8x\.i:8x;"
}</
{{out}}
Line 1,921 ⟶ 2,139:
=={{header|LFE}}==
<
(lambda (x)
(: io format '"~p~n" (list (: erlang integer_to_list x 8))))
(: lists seq 0 2000))
</syntaxhighlight>
=={{header|Liberty BASIC}}==
Terminate these ( essentially, practically) infinite loops by hitting <CTRL<BRK>
<syntaxhighlight lang="lb">
'the method used here uses the base-conversion from RC Non-decimal radices/Convert
'to terminate hit <CTRL<BRK>
Line 1,956 ⟶ 2,174:
toBase$ =right$( " " +toBase$, 10)
end function
</syntaxhighlight>
As suggested in LOGO, it is easy to work on a string representation too.
<syntaxhighlight lang="lb">
op$ = "00000000000000000000"
L =len( op$)
Line 1,989 ⟶ 2,207:
end
</syntaxhighlight>
Or use a recursive listing of permutations with the exception that the first digit is not 0 (unless listing single-digit numbers). For each digit-place, list numbers with 0-7 in the next digit-place.
<
i = 0
while 1
Line 2,008 ⟶ 2,226:
next i
end sub
</syntaxhighlight>
=={{header|Logo}}==
No built-in octal-formatting, so it's probably more efficient to just manually increment a string than to increment a number and then convert the whole thing to octal every time we print. This also lets us keep counting as long as we have room for the string.
<
ifelse [empty? :n] [
output 1
Line 2,034 ⟶ 2,252:
print :oct
make "oct increment_octal :oct
]</
=={{header|LOLCODE}}==
LOLCODE has no conception of octal numbers, but we can use string concatenation (<tt>SMOOSH</tt>) and basic arithmetic to accomplish the task.
<
HOW IZ I octal YR num
Line 2,056 ⟶ 2,274:
IM OUTTA YR printer
KTHXBYE</
=={{header|Lua}}==
<
print(string.format("%o",l))
end</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CountInOctal {
class OctalDigitsCounter {
private:
dim m() as byte
public:
Last as boolean
Value {
string s1
integer i
for i=len(.m()) to 1
s1=s1+.m(i)
next
=s1
}
Set (s as OctalDigitsCounter) {
.m()=s.m()
.Last<=false
}
Operator "++" {
integer z=0, i=1, h=len(.m()), L=h+1
if valid(.last) else error "problem"
while i<L
if .m(i)>=7% then z++:.m(i)=0:i++ else L=i:.m(i)++
end while
if z=H then .last<=true
}
class:
Module OctalDigitsCounter(Digits as long) {
if Digits<1 then Error "Wrong number for Digits"
dim .m(1 to Digits)
}
}
// set digits to number of character width of console
k=OctalDigitsCounter(width)
// or set to 3 digits
Rem : k=OctalDigitsCounter(3)
// you can press Esc to stop it
escape off
refresh 100 // to synchronize the scrolling, so we always see the numbers not the empty line
while not k.last
print part $(0), k // print without new line, $(0) used for non proportional character printing
refresh // so we do a refresh here before the scrolling which do the next print statement
print
k++
if keypress(27) then exit
end while
print
escape on
print "done"
}
CountInOctal
</syntaxhighlight>
=={{header|M4}}==
<
`ifelse($#,0,``$0'',
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',eval($2+$3),$3,`$4')')')dnl
forever(`y',0,1, `eval(y,8)
')</
=={{header|Maple}}==
<syntaxhighlight lang="maple">
octcount := proc (n)
seq(printf("%a \n", convert(i, octal)), i = 1 .. n);
end proc;
</syntaxhighlight>
=={{header|MACRO-10}}==
<syntaxhighlight lang="macro-10">
title OCTAL - Count in octal.
subttl PDP-10 assembly (MACRO-10 on TOPS-20). KJX 2022.
Line 2,116 ⟶ 2,390:
end start
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">x=0;
While[True,Print[BaseForm[x,8];x++]</
=={{header|MATLAB}} / {{header|Octave}}==
<
while (1)
dec2base(n,8)
n = n+1;
end; </
Or use printf:
<
while (1)
printf('%o\n',n);
n = n+1;
end; </
If a predefined sequence should be displayed, one can use
<
dec2base(seq,8)</
or
<
=={{header|Mercury}}==
<syntaxhighlight lang="text">
:- module count_in_octal.
:- interface.
Line 2,160 ⟶ 2,434:
io.format("%o\n", [i(N)], !IO),
count_in_octal(N + 1, !IO).
</syntaxhighlight>
=={{header|min}}==
{{works with|min|0.19.3}}
min has no support for octal or base conversion (it is a minimalistic language, after all) so we need to do that ourselves.
<
(dup 0 ==) (pop () 0 shorten)
(((8 mod) (8 div)) cleave) 'cons linrec
Line 2,172 ⟶ 2,446:
0 (dup octal succ)
9.223e18 int times ; close to max int value</
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
toOctal = function(n)
result = ""
while n != 0
octet = n % 8
n = floor(n / 8)
result = octet + result
end while
return result
end function
maxnum = 10 ^ 15 - 1
i = 0
while i < maxnum
i += 1
print i + " = " + toOctal(i)
end while
</syntaxhighlight>
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">ИП0 П1 1 0 / [x] П1 Вx {x} 1
0 * 7 - x=0 21 ИП1 x#0 28 БП
02 ИП0 1 + П0 С/П БП 00 ИП0 lg
[x] 1 + 10^x П0 С/П БП 00</
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 2,193 ⟶ 2,487:
INC (num)
UNTIL num = 0
END octal.</
=={{header|Nanoquery}}==
{{trans|C}}
Even though all integers are arbitrary precision, the maximum value that can be represented as octal using the format function is 2^64 - 1. Once this value is reached, the program terminates.
<
while i < 2^64
println format("%o", i)
i += 1
end</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols binary
Line 2,220 ⟶ 2,514:
k_ = k_.add(BigInteger.ONE)
end
</syntaxhighlight>
=={{header|NewLISP}}==
<
; url: http://rosettacode.org/wiki/Count_in_octal
; author: oofoe 2012-01-29
Line 2,233 ⟶ 2,527:
(for (i 0 (pow 2 32)) (println (format "%o" i)))
(exit)</
Sample output:
Line 2,252 ⟶ 2,546:
=={{header|Nim}}==
<
for i in 0 ..< int.high:
echo toOct(i, 16)</
=={{header|Oberon-2}}==
{{works with|oo2c}}
<
MODULE CountInOctal;
IMPORT
Line 2,271 ⟶ 2,565:
END
END CountInOctal.
</syntaxhighlight>
{{out}}
<pre>
Line 2,315 ⟶ 2,609:
=={{header|OCaml}}==
<
for i = 0 to max_int do
Printf.printf "%o\n" i
done</
{{out}}
Line 2,343 ⟶ 2,637:
Manual:
<
n=0;while(1,oct(n);n++)</
Automatic:
{{works with|PARI/GP|2.4.3 and above}}
<
=={{header|Pascal}}==
Line 2,354 ⟶ 2,648:
old string incrementer for Turbo Pascal transformed, same as in http://rosettacode.org/wiki/Count_in_octal#Logo, about 100x times faster than Dephi-Version, with the abilty to used preformated strings leading zeroes.
Added a Bit fiddling Version IntToOctString, nearly as fast.
<
{$Mode Delphi}
{$Optimization ON}
Line 2,450 ⟶ 2,744:
writeln('IntToOctString ',T1:8:3);
end.
</syntaxhighlight>
{{out}}
<pre> 1 1
Line 2,492 ⟶ 2,786:
IncStr 0.944 secs
IntToOctString 2.218 secs</pre>
===A recursive approach===
<syntaxhighlight lang="pascal">
program OctalCount;
{$mode objfpc}{$H+}
var
i : integer;
// display n in octal on console
procedure PutOctal(n : integer);
var
digit, n3 : integer;
begin
n3 := n shr 3;
if n3 <> 0 then PutOctal(n3);
digit := n and 7;
write(digit);
end;
// count in octal until integer overflow
begin
i := 1;
while i > 0 do
begin
PutOctal(i);
writeln;
i := i + 1;
end;
readln;
end.
</syntaxhighlight>
{{out}}
Showing last 10 lines of output
<pre>
17777777766
17777777767
17777777770
17777777771
17777777772
17777777773
17777777774
17777777775
17777777776
17777777777
</pre>
=={{header|Perl}}==
Since task says "system register", I take it to mean "no larger than machine native integer limit":
<
printf "%o\n", $_ for (0 .. POSIX::UINT_MAX);</
Otherwise:
<
my $i = 0;
printf "%o\n", $i++ while 1</
The above count in binary or decimal and convert to octal.
This actually counts in octal.
It will run forever or until the universe ends, whichever comes first.
<
$_ = 0;
s/([^7])?(7*)$/ $1 + 1 . $2 =~ tr!7!0!r /e while print "$_\n";</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 2,518 ⟶ 2,859:
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
See [[Integer_sequence#Phix]] for something that will run in a browser, obviously use "%o" instead of "%d" to
make it display octal numbers, or more accurately in that case, mpz_get_str(i,8). <!--(phixonline)-->
=={{header|PHP}}==
<
for ($n = 0; is_int($n); $n++) {
echo decoct($n), "\n";
}
?></
=={{header|Picat}}==
Line 2,536 ⟶ 2,877:
<
gen(N),
println(to_oct_string(N)),
Line 2,546 ⟶ 2,887:
gen(I, J) :-
I2 is I + 1,
gen(I2, J).</
{{out}}
Line 2,569 ⟶ 2,910:
=={{header|PicoLisp}}==
<
(prinl (oct N)) )</
=={{header|Pike}}==
<syntaxhighlight lang="pike">
int i=1;
while(true)
write("0%o\n", i++);
</syntaxhighlight>
{{Out}}
<pre>
Line 2,587 ⟶ 2,928:
=={{header|PL/I}}==
Version 1:
<
count: procedure options (main);
declare v(5) fixed(1) static initial ((5)0);
Line 2,610 ⟶ 2,951:
end inc;
end count;</
Version 2:
<
declare (i, j) fixed binary;
Line 2,622 ⟶ 2,963:
end;
end count;</
{{out}}
Line 2,646 ⟶ 2,987:
00000001215
00000001216
</pre>
==={{header|PL/I-80}}===
If you only need to count, and aren't bothered by leading zeroes in the output, this will do the trick simply and with a minimum of fuss.
<syntaxhighlight lang="pl/i">
octal_count:
procedure options (main);
dcl i fixed;
i = 1;
do while (i ^= 0);
put skip edit (unspec(i)) (b3);
i = i + 1;
end;
end octal_count;
</syntaxhighlight>
{{out}}
First and last 10 numbers of output
<pre>
000001
000002
000003
000004
000005
000006
000007
000010
000011
000012
...
177766
177767
177770
177771
177772
177773
177774
177775
177776
177777
</pre>
But a general purpose function to return the octal representation of an integer value as a string (similar to the OCT$ function in many BASICs) may prove more useful.<syntaxhighlight lang="pl/i">
octal_count:
procedure options (main);
dcl i fixed;
i = 1;
do while (i ^= 0);
put skip list (octal(i));
i = i + 1;
end;
stop;
octal:
procedure (n) returns (char(6) varying);
dcl
(n, m) fixed,
s char(6) varying;
/* n is passed by reference, so make a local copy */
m = n;
s = '';
do while (m > 0);
s = ascii(mod(m,8) + rank('0')) || s;
m = m / 8;
end;
return (s);
end octal;
end octal_count;
</syntaxhighlight>
{{out}}
First and last 10 numbers of output
<pre>
1
2
3
4
5
6
7
10
11
12
...
77766
77767
77770
77771
77772
77773
77774
77775
77776
77777
</pre>
=={{header|PL/M}}==
<
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
DECLARE FN BYTE, ARG ADDRESS;
Line 2,678 ⟶ 3,113:
CALL PR$NL;
END;
EOF</
=={{header|PowerShell}}==
<
While ( $True )
{
[Convert]::ToString( ++$i, 8 )
}</
=={{header|Prolog}}==
Line 2,692 ⟶ 3,127:
octalize will keep producing and printing octal number, there is no limit.
<
octal([O]) :- o(O).
Line 2,705 ⟶ 3,140:
octal(X),
(maplist(write, X), nl)
).</
=={{header|PureBasic}}==
<
Static Dim digits(20)
Protected i, j, result.s
Line 2,735 ⟶ 3,170:
CloseConsole()
EndIf
</syntaxhighlight>
Sample output:
<pre>0
Line 2,760 ⟶ 3,195:
=={{header|Python}}==
===Python2===
<syntaxhighlight lang="python">import sys
for n in xrange(sys.maxint):
print oct(n)</
===Python3===
<syntaxhighlight lang="python">
# Python3 count_in_oct.py by Xing216
import sys
for n in range(sys.maxsize):
print(oct(n))
</syntaxhighlight>
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
Dim As Integer iNum, Icount
Dim sMax As String
Line 2,780 ⟶ 3,223:
REM also QBasic example runs under QB64
</syntaxhighlight>
=={{header|Quackery}}==
<
0 [ dup echo cr 1+ again ]</
=={{header|Racket}}==
<
#lang racket
(for ([i (in-naturals)])
(displayln (number->string i 8)))
</syntaxhighlight>
(Racket has bignums, so this loop will never end.)
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
{{out}}
<pre>0</pre>
Line 2,805 ⟶ 3,248:
The technique used is to convert the decimal number to binary, and separate the binary digits in groups of three, and then convert those binary groups (numbers) to decimal.
<
/*┌────────────────────────────────────────────────────────────────────┐
Line 2,836 ⟶ 3,279:
if #>sourceline() then leave /*stop if # of protons > pgm statements*/
end /*#*/
/*stick a fork in it, we're all done. */</
{{out|output}}
<pre style="height:38ex">
Line 2,876 ⟶ 3,319:
=={{header|Ring}}==
<
size = 30
for n = 1 to size
Line 2,891 ⟶ 3,334:
end
return output
</syntaxhighlight>
=={{header|RPL}}==
This will run an octal counter at the top of the screen from 1 to n, n being entered as an argument.
≪ OCT CLLCD
1 SWAP '''FOR''' j
j R→B 1 DISP 0.2 WAIT '''NEXT'''
CLMF
≫ 'CLOCT' STO
=={{header|Ruby}}==
<
loop do
puts "%o" % n
Line 2,913 ⟶ 3,364:
0.step do |n|
puts format("%o", n)
end</
=={{header|Run BASIC}}==
<
input " End number:";e
Line 2,930 ⟶ 3,381:
if base10 < 1 then exit for
next i
end function</
=={{header|Rust}}==
<
for i in 0..std::usize::MAX {
println!("{:o}", i);
}
}</
=={{header|Salmon}}==
Line 2,943 ⟶ 3,394:
Salmon has built-in unlimited-precision integer arithmetic, so these examples will all continue printing octal values indefinitely, limited only by the amount of memory available (it requires O(log(n)) bits to store an integer n, so if your computer has 1 GB of memory, it will count to a number with on the order of <math>2^{80}</math> octal digits).
<
printf("%o%\n", i);;</
or
<
printf("%o%\n", i);;</
or
<
while (true)
{
printf("%o%\n", i);
++i;
};</
=={{header|S-BASIC}}==
Although many BASICs have a built-in OCT$ function, S-BASIC does not, so we have to supply our own
<syntaxhighlight lang="basic">
rem - return p mod q
function mod(p, q = integer) = integer
Line 2,988 ⟶ 3,439:
end
</syntaxhighlight>
{{out}}
Showing first and last 10 lines of output
Line 3,016 ⟶ 3,467:
=={{header|Scala}}==
<
=={{header|Scheme}}==
<
=={{header|Scratch}}==
[[File:ScratchCountInOctal.png]]
=={{header|sed}}==
This program expects one line (consisting of a non-negative octal integer) as start value:
<syntaxhighlight lang="sed">:l
p
s/^7*$/0&/
h
y/01234567/12345670/
x
G
s/.7*\n.*\([^0]\)/\1/
bl</syntaxhighlight>
{{out}}
<pre>
$ echo 0 | sed -f count_oct.sed | head
0
1
2
3
4
5
6
7
10
11
</pre>
=={{header|Seed7}}==
This example uses the [http://seed7.sourceforge.net/libraries/integer.htm#%28in_integer%29radix%28in_integer%29 radix] operator to write a number in octal.
<
const proc: main is func
Line 3,037 ⟶ 3,514:
incr(i);
until FALSE;
end func;</
=={{header|Sidef}}==
<
loop { say i++.as_oct }</
=={{header|Simula}}==
<
BEGIN
Line 3,069 ⟶ 3,546:
END;
END.
</syntaxhighlight>
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<
n printOn:Stdout radix:8.
Stdout cr.
]</
{{out}}
<pre>1
Line 3,098 ⟶ 3,575:
=={{header|Sparkling}}==
<
printf("%o\n", i);
}</
=={{header|Standard ML}}==
<
fun count n = (print (Int.fmt StringCvt.OCT n ^ "\n"); count (n+1))
in
val _ = count 0
end</
=={{header|Swift}}==
<
func octalSuccessor(value: String) -> String {
Line 3,137 ⟶ 3,614:
println(n)
n = octalSuccessor(n)
}</
{{Output}}
Line 3,163 ⟶ 3,640:
=={{header|Tcl}}==
<
while 1 {
puts [format "%llo" [incr counter]]
}</
=={{header|UNIX Shell}}==
We use the bc calculator to increment our octal counter:
<
num=0
while true; do
echo $num
num=`echo "obase=8;ibase=8;$num+1"|bc`
done</
===Using printf ===
Increment a decimal counter and use <code>printf(1)</code> to print it in octal. Our loop stops when the counter overflows to negative.
<
num=0
while test 0 -le $num; do
printf '%o\n' $num
num=`expr $num + 1`
done</
Various recent shells have a bultin <code>$(( ... ))</code> for arithmetic rather than running <code>expr</code>, in which case
Line 3,192 ⟶ 3,669:
{{works with|bash}}
{{works with|pdksh|5.2.14}}
<
while test 0 -le $num; do
printf '%o\n' $num
num=$((num + 1))
done</
=={{header|VBA}}==
Line 3,202 ⟶ 3,679:
With i defined as an Integer, the loop will count to 77777 (32767 decimal). Error handling added to terminate nicely on integer overflow.
<syntaxhighlight lang="vba">
Sub CountOctal()
Dim i As Integer
Line 3,214 ⟶ 3,691:
Debug.Print "Integer overflow - count terminated"
End Sub
</syntaxhighlight>
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
For i = 0 To 20
WScript.StdOut.WriteLine Oct(i)
Next
</syntaxhighlight>
=={{header|Vim Script}}==
<
while counter >= 0
echon printf("%o\n", counter)
let counter += 1
endwhile</
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math
fn main() {
for i := i8(0); ; i++ {
Line 3,239 ⟶ 3,716:
}
}
}</
{{out}}
<pre>
Line 3,255 ⟶ 3,732:
=={{header|VTL-2}}==
Stops at 65535, the largest integer supported by VTL-2.
<
1010 #=2000
1020 ?=""
Line 3,272 ⟶ 3,749:
2090 E=E-1
2100 #=E>1*2080
2110 #=R</
{{out}}
<pre>
Line 3,296 ⟶ 3,773:
This program prints octal numbers until the internal representation of the current integer overflows to -1; it will never do so on some interpreters.
<syntaxhighlight lang="whitespace">
Line 3,335 ⟶ 3,812:
</
It was generated from the following pseudo-Assembly.
<
; Increment indefinitely.
0:
Line 3,371 ⟶ 3,848:
3:
pop
ret</
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var i = 0
Line 3,381 ⟶ 3,858:
System.print(Conv.oct(i))
i = i + 1
}</
{{out}}
Line 3,411 ⟶ 3,888:
=={{header|XPL0}}==
XPL0 doesn't have built-in routines to handle octal; instead it uses hex.
<
proc OctOut(N); \Output N in octal
Line 3,427 ⟶ 3,904:
I:= I+1;
until KeyHit or I=0;
]</
Example output:
Line 3,452 ⟶ 3,929:
=={{header|zig}}==
<
const fmt = std.fmt;
const warn = std.debug.warn;
Line 3,464 ⟶ 3,941:
warn("{}\n", buf);
}
}</
=={{header|Z80 Assembly}}==
Line 3,471 ⟶ 3,948:
Outputs octal values 0 through 77 (decimal 0 to 63, or hexadecimal 0x00 to 0x3F.)
<
ld b,&40 ;how many numbers to print.
loop_showOctal:
Line 3,529 ⟶ 4,006:
add a,&F0
adc a,&40 ;this sequence of instructions converts hexadecimal values to ASCII.
jp PrintChar ;hardware-specific routine, omitted. Thanks to Keith of Chibiakumas for this one!</
{{out}}
Line 3,544 ⟶ 4,021:
=={{header|zkl}}==
<
{{out}}
<pre>
Line 3,561 ⟶ 4,038:
=={{header|ZX Spectrum Basic}}==
<
20 FOR i=0 TO 20
30 LET o$="": LET n=i
Line 3,570 ⟶ 4,047:
80 NEXT i
90 STOP
100 DEF FN m(a,b)=a-INT (a/b)*b</
|