text stringlengths 0 1.19k |
|---|
dec rdi ; n -= 1 |
jmp .loop |
.done: |
ret |
_start: |
mov rdi, 5 ; compute 5! |
call factorial ; rax now holds 120 |
; exit(0) |
mov rax, 60 |
xor rdi, rdi |
syscall |
``` |
Assembly has no built-in loops or functions — you build them from comparisons and jumps. Everything is explicit: registers, memory, control flow. |
## Java |
```java |
public class Factorial { |
public static long factorial(int n) { |
long result = 1; |
for (int i = 2; i <= n; i++) { |
result *= i; |
} |
return result; |
} |
public static void main(String[] args) { |
System.out.println(factorial(5)); // 120 |
} |
} |
``` |
Java wraps everything in classes, uses garbage collection, and has real for-loops and type safety built in. |
## Fortran |
```fortran |
program factorial_demo |
implicit none |
integer :: n |
integer(kind=8) :: result |
n = 5 |
result = factorial(n) |
print *, result |
contains |
function factorial(n) result(res) |
integer, intent(in) :: n |
integer(kind=8) :: res |
integer :: i |
res = 1 |
do i = 2, n |
res = res * i |
end do |
end function factorial |
end program factorial_demo |
``` |
Fortran (still widely used in scientific/numerical computing) uses `program`/`end program` blocks, explicit `integer(kind=8)` for 64-bit ints, and `do` loops instead of `for`. |
--- |
Want me to do this same comparison for something else — like sorting an array, string reversal, or reading user input? Or if you had a specific algorithm in mind, tell me and I'll do all three languages for it. |
Please provide the code for each language: COBOL, Pascal, Prolog, Lisp. about the different things you choose-I'll show classic **FizzBuzz** in each language — a simple, universally understood example that highlights each language's distinct style. |
## COBOL |
```cobol |
IDENTIFICATION DIVISION. |
PROGRAM-ID. FIZZBUZZ. |
DATA DIVISION. |
WORKING-STORAGE SECTION. |
01 WS-NUM PIC 9(3). |
PROCEDURE DIVISION. |
PERFORM VARYING WS-NUM FROM 1 BY 1 UNTIL WS-NUM > 20 |
IF FUNCTION MOD(WS-NUM, 15) = 0 |
DISPLAY "FizzBuzz" |
ELSE IF FUNCTION MOD(WS-NUM, 3) = 0 |
DISPLAY "Fizz" |
ELSE IF FUNCTION MOD(WS-NUM, 5) = 0 |
DISPLAY "Buzz" |
ELSE |
DISPLAY WS-NUM |
END-IF |
END-PERFORM |
STOP RUN. |
``` |
Verbose and English-like by design — COBOL was built for business readability, not brevity. |
## Pascal |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.