Pla$orm ¡independence ¡and ¡languages ¡
CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡
Pla$orm independence and languages CSCI 136: Fundamentals of - - PowerPoint PPT Presentation
Pla$orm independence and languages CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Language types: Compiled e.g. C++
CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡
2 ¡
... ¡ for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ { ¡ ¡ ¡ ¡int ¡j ¡= ¡2; ¡ ¡ ¡ ¡while ¡((j ¡< ¡i) ¡&& ¡(i ¡% ¡j ¡!= ¡0)) ¡ ¡ ¡ ¡ ¡ ¡ ¡j++; ¡ ¡ ¡ ¡if ¡(j ¡== ¡i) ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("%d ¡", ¡i); ¡ } ¡ ... ¡
5 ¡
public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡
6 ¡
¡ public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡
#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ #include ¡<math.h> ¡
¡
int ¡main(int ¡argc, ¡char** ¡argv) ¡ { ¡ ¡ ¡ ¡const ¡int ¡N ¡= ¡atoi(argv[1]); ¡ ¡ ¡ ¡ ¡bool* ¡a ¡= ¡new ¡bool[N ¡+ ¡1]; ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡sqrt((double) ¡N); ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡n) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡printf("\n"); ¡ ¡ ¡ ¡ ¡delete ¡[] ¡a; ¡ ¡ ¡ ¡return ¡0; ¡ } ¡
7 ¡
hNp://xkcd.com/303/ ¡
#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ #include ¡<math.h> ¡
¡
int ¡main(int ¡argc, ¡char** ¡argv) ¡ { ¡ ¡ ¡ ¡int ¡n ¡= ¡atoi(argv[1]); ¡ ¡ ¡ ¡... ¡
8 ¡
#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ #include ¡<math.h> ¡
¡
int ¡main(int ¡argc, ¡char** ¡argv) ¡ { ¡ ¡ ¡ ¡int ¡n ¡= ¡atoi(argv[1]); ¡ ¡ ¡ ¡... ¡
9 ¡
10 ¡
11 ¡
12 ¡
; Parameters to our ArrayInit subroutine ArrayNum EQU 30h ; Where our array starts ArrayMemStart EQU 31h ; 1st memory location in the array ArrayInitVal EQU 32h ; What value to load into array Start: MOV ArrayNum, #10 MOV ArrayMemStart, #40h MOV ArrayInitVal, #0ABh CALL ArrayInit JMP Start ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Init the memory in an array to a value, uses R0 and R1 ArrayInit: MOV R0, ArrayNum MOV R1, ArrayMemStart ArrayInitLoop: MOV @R1, ArrayInitVal INC R1 DJNZ R0, ArrayInitLoop RET
public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡n ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡
☼LineNumberTable☺ ♦main☺ ▬([Ljava/lang/String;)V☺ StackMapTable #☺ SourceFile☺ ♂Primes.java♀ ♂ ♀ *♀ + ,☺ ♥%d ☺ ►java/lang/Object♀
This ¡guy ¡will ¡run ¡on ¡ anything ¡with ¡a ¡Java ¡ Virtual ¡Machine ¡(JVM) ¡
13 ¡
14 ¡
15 ¡
16 ¡
¡ public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡ using ¡System; ¡ ¡ namespace ¡Primes ¡ { ¡ ¡ ¡ ¡class ¡Primes ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡static ¡void ¡Main(string[] ¡args) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡n ¡= ¡Int32.Parse(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡bool[] ¡a ¡= ¡new ¡bool[n ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡n; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.Sqrt((double) ¡n); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡n) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡n; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.Console.Write(i ¡+ ¡" ¡"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.Console.WriteLine(); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ } ¡
17 ¡
18 ¡
19 ¡
¡ public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡
<?php ¡ ¡ ¡ ¡ ¡ $n ¡= ¡$_GET["N"]; ¡ ¡ $a ¡= ¡array(); ¡ for ¡($i ¡= ¡0; ¡$i ¡<= ¡$n; ¡$i++) ¡ ¡ ¡ ¡ ¡$a[$i] ¡= ¡true; ¡ ¡ $top ¡= ¡intval(sqrt($n)); ¡ for ¡($i ¡= ¡2; ¡$i ¡<= ¡$top; ¡$i++) ¡ { ¡ ¡ ¡ ¡ ¡$j ¡= ¡2 ¡* ¡$i; ¡ ¡ ¡ ¡ ¡while ¡($j ¡<= ¡$n) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$a[$j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$j ¡+= ¡$i; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡($i ¡= ¡2; ¡$i ¡<= ¡$n; ¡$i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡($a[$i]) ¡echo ¡"$i ¡"; ¡ ¡ ¡ ¡ ¡echo ¡"<br ¡/>"; ¡ ¡ ¡ ¡ ¡ } ¡ ¡ ?> ¡
20 ¡
21 ¡
22 ¡