diff --git a/arm_asm/main.s b/arm_asm/main.s new file mode 100644 index 0000000..54c6e37 --- /dev/null +++ b/arm_asm/main.s @@ -0,0 +1,160 @@ + + + +.equ STDOUT, 1 @ Linux output console +.equ EXIT, 1 @ Linux syscall +.equ WRITE, 4 @ Linux syscall +.equ MAXI, 22 + +.data +sMessValeur: .fill 11, 1, ' ' @ size => 11 +szCarriageReturn: .asciz "\n" +sBlanc1: .asciz " " +sBlanc2: .asciz " " +sBlanc3: .asciz " " + +.bss + +.text +.global main +main: @ entry of program + push {fp,lr} @ saves 2 registers + @ display first line + mov r4,#0 +1: @ begin loop + mov r0,r4 + ldr r1,iAdrsMessValeur @ display value + bl conversion10 @ call function + mov r2,#0 @ final zéro + strb r2,[r1,r0] @ on display value + ldr r0,iAdrsMessValeur + bl affichageMess @ display message + cmp r4,#10 @ one or two digit in résult + ldrgt r0,iAdrsBlanc2 @ two display two spaces + ldrle r0,iAdrsBlanc3 @ one display 3 spaces + bl affichageMess @ display message + add r4,#1 @ increment counter + cmp r4,#MAXI + ble 1b @ loop + ldr r0,iAdrszCarriageReturn + bl affichageMess @ display carriage return + + mov r5,#1 @ line counter +2: @ begin loop lines + mov r0,r5 @ display column 1 with N° line + ldr r1,iAdrsMessValeur @ display value + bl conversion10 @ call function + mov r2,#0 @ final zéro + strb r2,[r1,r0] + ldr r0,iAdrsMessValeur + bl affichageMess @ display message + cmp r5,#10 @ one or two digit in N° line + ldrge r0,iAdrsBlanc2 + ldrlt r0,iAdrsBlanc3 + bl affichageMess + mov r4,#1 @ counter column +3: @ begin loop columns + mul r0,r4,r5 @ multiplication + mov r3,r0 @ save résult + ldr r1,iAdrsMessValeur @ display value + bl conversion10 @ call function + mov r2,#0 + strb r2,[r1,r0] + ldr r0,iAdrsMessValeur + bl affichageMess @ display message + cmp r3,#100 @ 3 digits in résult ? + ldrge r0,iAdrsBlanc1 @ yes, display one space + bge 4f + cmp r3,#10 @ 2 digits in result + ldrge r0,iAdrsBlanc2 @ yes display 2 spaces + ldrlt r0,iAdrsBlanc3 @ no display 3 spaces +4: + bl affichageMess @ display message + add r4,#1 @ increment counter column + cmp r4,r5 @ < counter lines + ble 3b @ loop + ldr r0,iAdrszCarriageReturn + bl affichageMess @ display carriage return + add r5,#1 @ increment line counter + cmp r5,#MAXI @ MAXI ? + ble 2b @ loop + +100: @ standard end of the program + mov r0, #0 @ return code + pop {fp,lr} @restaur 2 registers + mov r7, #EXIT @ request to exit program + svc #0 @ perform the system call + +iAdrsMessValeur: .int sMessValeur +iAdrszCarriageReturn: .int szCarriageReturn +iAdrsBlanc1: .int sBlanc1 +iAdrsBlanc2: .int sBlanc2 +iAdrsBlanc3: .int sBlanc3 + +affichageMess: + push {r0,r1,r2,r7,lr} @ save registres + mov r2,#0 @ counter length +1: @ loop length calculation + ldrb r1,[r0,r2] @ read octet start position + index + cmp r1,#0 @ if 0 its over + addne r2,r2,#1 @ else add 1 in the length + bne 1b @ and loop + @ so here r2 contains the length of the message + mov r1,r0 @ address message in r1 + mov r0,#STDOUT @ code to write to the standard output Linux + mov r7, #WRITE @ code call system "write" + svc #0 @ call systeme + pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */ + bx lr @ return + +.equ LGZONECAL, 10 +conversion10: + push {r1-r4,lr} @ save registers + mov r3,r1 + mov r2,#LGZONECAL + +1: @ start loop + bl divisionpar10U @unsigned r0 <- dividende. quotient ->r0 reste -> r1 + add r1,#48 @ digit + strb r1,[r3,r2] @ store digit on area + cmp r0,#0 @ stop if quotient = 0 */ + subne r2,#1 @ else previous position + bne 1b @ and loop + @ and move digit from left of area + mov r4,#0 +2: + ldrb r1,[r3,r2] + strb r1,[r3,r4] + add r2,#1 + add r4,#1 + cmp r2,#LGZONECAL + ble 2b + @ and move spaces in end on area + mov r0,r4 @ result length + mov r1,#' ' @ space +3: + strb r1,[r3,r4] @ store space in area + add r4,#1 @ next position + cmp r4,#LGZONECAL + ble 3b @ loop if r4 <= area size + +100: + pop {r1-r4,lr} @ restaur registres + bx lr @return + + +divisionpar10U: + push {r2,r3,r4, lr} + mov r4,r0 @ save value + mov r3,#0xCCCD @ r3 <- magic_number lower + movt r3,#0xCCCC @ r3 <- magic_number upper + umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0) + mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3 + add r2,r0,r0, lsl #2 @ r2 <- r0 * 5 + sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) + pop {r2,r3,r4,lr} + bx lr @ leave function + + + + diff --git a/cpp/main.cpp b/cpp/main.cpp new file mode 100644 index 0000000..d5bb635 --- /dev/null +++ b/cpp/main.cpp @@ -0,0 +1,71 @@ +#include +#include +#include // for log10() +#include // for max() + +size_t table_column_width(const int min, const int max) +{ + unsigned int abs_max = std::max(max*max, min*min); + + size_t colwidth = 2 + std::log10(abs_max); + + if (min < 0 && max > 0) + ++colwidth; + return colwidth; +} + +struct Writer_ +{ + decltype(std::setw(1)) fmt_; + Writer_(size_t w) : fmt_(std::setw(w)) {} + template Writer_& operator()(const T_& info) { std::cout << fmt_ << info; return *this; } +}; + +void print_table_header(const int min, const int max) +{ + Writer_ write(table_column_width(min, max)); + + // table corner + write(" "); + for(int col = min; col <= max; ++col) + write(col); + + // End header with a newline and blank line. + std::cout << std::endl << std::endl; +} + +void print_table_row(const int num, const int min, const int max) +{ + Writer_ write(table_column_width(min, max)); + + // Header column + write(num); + + // Spacing to ensure only the top half is printed + for(int multiplicand = min; multiplicand < num; ++multiplicand) + write(" "); + + // Remaining multiplicands for the row. + for(int multiplicand = num; multiplicand <= max; ++multiplicand) + write(num * multiplicand); + + // End row with a newline and blank line. + std::cout << std::endl << std::endl; +} + +void print_table(const int min, const int max) +{ + // Header row + print_table_header(min, max); + + // Table body + for(int row = min; row <= max; ++row) + print_table_row(row, min, max); +} + +int main() +{ + print_table(1, 22); + return 0; +} + diff --git a/fsharp/main.fs b/fsharp/main.fs new file mode 100644 index 0000000..45685ad --- /dev/null +++ b/fsharp/main.fs @@ -0,0 +1,19 @@ +open System + +let multTable () = + Console.Write (" X".PadRight (4)) + for i = 1 to 22 do Console.Write ((i.ToString "####").PadLeft 4) + Console.Write "\n ___" + for i = 1 to 22 do Console.Write " ___" + Console.WriteLine () + for row = 1 to 22 do + Console.Write (row.ToString("###").PadLeft(3).PadRight(4)) + for col = 1 to 22 do + if row <= col then Console.Write ((row * col).ToString("###").PadLeft(4)) + else + Console.Write ("".PadLeft 4) + Console.WriteLine () + Console.WriteLine () + Console.ReadKey () |> ignore + +multTable () diff --git a/golang/main.go b/golang/main.go new file mode 100644 index 0000000..cbd1143 --- /dev/null +++ b/golang/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Print(" x |") + for i := 1; i <= 22; i++ { + fmt.Printf("%4d", i) + } + fmt.Print("\n---+") + for i := 1; i <= 22; i++ { + fmt.Print("----") + } + for j := 1; j <= 2; j++ { + fmt.Printf("\n%2d |", j) + for i := 1; i <= 22; i++ { + if i >= j { + fmt.Printf("%4d", i*j) + } else { + fmt.Print(" ") + } + } + } + fmt.Println("") +} + diff --git a/java/main.java b/java/main.java new file mode 100644 index 0000000..6daedfa --- /dev/null +++ b/java/main.java @@ -0,0 +1,20 @@ +public class MultiplicationTable { + public static void main(String[] args) { + for (int i = 1; i <= 22; i++) + System.out.print("\t" + i); + + System.out.println(); + for (int i = 0; i < 100; i++) + System.out.print("-"); + System.out.println(); + for (int i = 1; i <= 22; i++) { + System.out.print(i + "|"); + for(int j = 1; j <= 22; j++) { + System.out.print("\t"); + if (j >= i) + System.out.print("\t" + i * j); + } + System.out.println(); + } + } +} diff --git a/lua/main.lua b/lua/main.lua new file mode 100644 index 0000000..f632f0f --- /dev/null +++ b/lua/main.lua @@ -0,0 +1,18 @@ +io.write( " |" ) +for i = 1, 22 do + io.write( string.format( "%#5d", i ) ) +end +io.write( "\n", string.rep( "-", 22*5+4 ), "\n" ) + +for i = 1, 22 do + io.write( string.format( "%#2d |", i ) ) + + for j = 1, 22 do + if j < i then + io.write( " " ) + else + io.write( string.format( "%#5d", i*j ) ) + end + end + io.write( "\n" ) +end \ No newline at end of file diff --git a/perl/main.pl b/perl/main.pl new file mode 100644 index 0000000..d106b66 --- /dev/null +++ b/perl/main.pl @@ -0,0 +1,10 @@ +our $max = 22; +our $width = length($max**2) + 1; + +printf "%*s", $width, $_ foreach 'x|', 1..$max; +print "\n", '-' x ($width - 1), '+', '-' x ($max*$width), "\n"; +foreach my $i (1..$max) { + printf "%*s", $width, $_ + foreach "$i|", map { $_ >= $i and $_*$i } 1..$max; + print "\n"; +} \ No newline at end of file diff --git a/powershell/main.ps1 b/powershell/main.ps1 new file mode 100644 index 0000000..de6573d --- /dev/null +++ b/powershell/main.ps1 @@ -0,0 +1,20 @@ +# For clarity +$Tab = "`t" + + +$Tab + ( 1..22 -join $Tab ) + + +ForEach ( $i in 1..22 ) + { + $( + $i + + @( "" ) * ( $i - 1 ) + + # Calculate + $i..22 | ForEach { $i * $_ } + + # Combine + ) -join $Tab + } \ No newline at end of file diff --git a/rlang/main.r b/rlang/main.r new file mode 100644 index 0000000..85b81d4 --- /dev/null +++ b/rlang/main.r @@ -0,0 +1,10 @@ +multiplication_table <- function(n=22) +{ + one_to_n <- 1:n + x <- matrix(one_to_n) %*% t(one_to_n) + x[lower.tri(x)] <- 0 + rownames(x) <- colnames(x) <- one_to_n + print(as.table(x), zero.print="") + invisible(x) +} +multiplication_table() \ No newline at end of file diff --git a/ruby/main.rb b/ruby/main.rb new file mode 100644 index 0000000..d360798 --- /dev/null +++ b/ruby/main.rb @@ -0,0 +1,12 @@ +def multiplication_table(n) + puts " |" + (" %3d" * n) % [*1..n] + puts "----+" + "----" * n + 1.upto(n) do |x| + print "%3d |" % x + 1.upto(x-1) {|y| print " "} + x.upto(n) {|y| print " %3d" % (x*y)} + puts + end +end + +multiplication_table 22 \ No newline at end of file diff --git a/rust/main.rs b/rust/main.rs new file mode 100644 index 0000000..89a0db5 --- /dev/null +++ b/rust/main.rs @@ -0,0 +1,17 @@ +fn main() { + let xs = (1..=22) + .map(|a| { + (1..=22) + .map(|b| { + if a > b { + String::from(" ") + } else { + format!("{:4}", a * b) + } + }) + .collect::() + }) + .collect::>(); + + println!("{}", xs.join("\n")) +} \ No newline at end of file diff --git a/scala/main.scl b/scala/main.scl new file mode 100644 index 0000000..cfb77ac --- /dev/null +++ b/scala/main.scl @@ -0,0 +1,17 @@ +print("%5s".format("|")) +for (i <- 1 to 22) print("%5d".format(i)) +println() +println("-----" * 23) + +for (i <- 1 to 22) { + print("%4d|".format(i)) + + for (j <- 1 to 22) { + if (i <= j) + print("%5d".format(i * j)) + else + print("%5s".format("")) + } + + println("") +} diff --git a/vb/main.vb b/vb/main.vb new file mode 100644 index 0000000..5e333ac --- /dev/null +++ b/vb/main.vb @@ -0,0 +1,24 @@ +Sub Main() + Const nmax = 22, xx = 3 + Const x = xx + 1 + Dim i As Integer, j As Integer, s As String + s = String(xx, " ") & " |" + For j = 1 To nmax + s = s & Right(String(x, " ") & j, x) + Next j + Debug.Print s + s = String(xx, "-") & " +" + For j = 1 To nmax + s = s & " " & String(xx, "-") + Next j + Debug.Print s + For i = 1 To nmax + s = Right(String(xx, " ") & i, xx) & " |" + For j = 1 To nmax + If j >= i _ + Then s = s & Right(String(x, " ") & i * j, x) _ + Else s = s & String(x, " ") + Next j + Debug.Print s + Next i +End Sub 'Main \ No newline at end of file