How to produce a sequential count in octal, starting at zero in Scala

1 Answer

0 votes
// Fixed‑width octal (e.g., always 3 digits) with reusable conversion function
// String.format works just like Java

object Main extends App {

  def toOctal(n: Int): String =
    String.format("%03o", Int.box(n))   // padded 3‑digit octal

  for (i <- 0 to 64) {
    println(f"$i%2d = ${toOctal(i)}")
  }
}


/*
run:

 0 = 0  
 1 = 1  
 2 = 2  
 3 = 3  
 4 = 4  
 5 = 5  
 6 = 6  
 7 = 7  
 8 = 10 
 9 = 11 
10 = 12 
11 = 13 
12 = 14 
13 = 15 
14 = 16 
15 = 17 
16 = 20 
17 = 21 
18 = 22 
19 = 23 
20 = 24 
21 = 25 
22 = 26 
23 = 27 
24 = 30 
25 = 31 
26 = 32 
27 = 33 
28 = 34 
29 = 35 
30 = 36 
31 = 37 
32 = 40 
33 = 41 
34 = 42 
35 = 43 
36 = 44 
37 = 45 
38 = 46 
39 = 47 
40 = 50 
41 = 51 
42 = 52 
43 = 53 
44 = 54 
45 = 55 
46 = 56 
47 = 57 
48 = 60 
49 = 61 
50 = 62 
51 = 63 
52 = 64 
53 = 65 
54 = 66 
55 = 67 
56 = 70 
57 = 71 
58 = 72 
59 = 73 
60 = 74 
61 = 75 
62 = 76 
63 = 77 
64 = 100

*/

 



answered Mar 24 by avibootz
...