Count in octal: Difference between revisions

No edit summary
 
(17 intermediate revisions by 12 users not shown)
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>
 
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>
 
Line 909 ⟶ 1,019:
Local (2) ' digit C@ and string D@
' initialize, save sign
d@ := Dup("") : Push a@ < 0 : a@ = Abs(a@)
Do
Line 1,311 ⟶ 1,421:
WriteLn(DecToOct(i));
end.</syntaxhighlight>
 
=={{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}}==
Line 1,723 ⟶ 1,859:
=={{header|J}}==
'''Solution:'''
<syntaxhighlight lang="j"> disp=. ([ echo) '1 '(-.~":) 8&#.inv
(1 + disp)^:_]0x</syntaxhighlight>
 
The full result is not displayable, by design. This could be considered a bug, but is an essential part of this task. Here's how it starts:
 
<syntaxhighlight lang="j"> (1 + disp)^:_]0x
0
1
Line 1,741 ⟶ 1,877:
...</syntaxhighlight>
 
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 and remove the delimiting spaces - this gives us the octal values we want to display.)
 
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,757 ⟶ 1,893:
>:&.(8&#.)1 0 0
1 0 1</syntaxhighlight>
 
=={{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="java">
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){
Line 1,868 ⟶ 2,052:
 
=={{header|langur}}==
We have to use an arbitrary limit for this.
 
We use the :8x interpolation modifier to create a string in base 8 (may use base 2 to 36).
Line 1,874 ⟶ 2,058:
<syntaxhighlight lang="langur">val .limit = 70000
 
for .i = 0; .i <=of .limit; .i += 1 {
writeln $"10x\.i; == 8x\.i:8x;"
}</syntaxhighlight>
Line 2,111 ⟶ 2,295:
integer i
for i=len(.m()) to 1
s1+=s1+.m(i)
next
=s1
Line 2,263 ⟶ 2,447:
0 (dup octal succ)
9.223e18 int times ; close to max int value</syntaxhighlight>
 
=={{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}}==
Line 2,584 ⟶ 2,788:
 
===A recursive approach===
For this task, recursion offers no advantage in clarity or efficiency over iteration, but it's nevertheless an instructive exercise.
<syntaxhighlight lang="pascal">
program OctalCount;
Line 2,992 ⟶ 3,195:
 
=={{header|Python}}==
===Python2===
<syntaxhighlight lang="python">import sys
for n in xrange(sys.maxint):
print oct(n)</syntaxhighlight>
===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}}==
Line 3,641 ⟶ 3,852:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv
 
var i = 0
885

edits