]> git.g-eek.se Git - inbyggd-frihet-wiki.git/commitdiff
[linux-intro] Complete example files
authorGustav Eek <gustav.eek@fripost.org>
Mon, 21 Jun 2021 11:44:05 +0000 (13:44 +0200)
committerGustav Eek <gustav.eek@fripost.org>
Mon, 21 Jun 2021 11:44:05 +0000 (13:44 +0200)
Add an example script file which computes prime numbers, and complete
with a C version for execution efficiency comparison.

linux-intro/files/files.sh
linux-intro/files/linux-intro.tar
linux-intro/files/prime-again.sh [new file with mode: 0644]
linux-intro/files/prime.c [new file with mode: 0644]
linux-intro/files/prime.sh [new file with mode: 0644]

index 35cf42bd4eca373aeca1625df67c5f274cd068c5..69c6c9502264e56ee5d85d4c56f0ed208c2cc1bd 100644 (file)
@@ -11,6 +11,8 @@ folders="\
   05.avsl \
 "
 
+# Include example text file to each lesson
+
 text=text.txt
 
 for f in $folders; do
@@ -21,5 +23,11 @@ for f in $folders; do
     done
 done
 
+# Include script examples to lesson 4
+
+cp prime.sh prime.c $file/04.stil
+
+# Compress and cleanup
+
 tar -cvf $file.tar $file
 rm -fr $file
index d89e45579d7a85c90bc51c874ced4f60bc896191..527562bbf95a090cbf7143a85a5de229b50abe22 100644 (file)
Binary files a/linux-intro/files/linux-intro.tar and b/linux-intro/files/linux-intro.tar differ
diff --git a/linux-intro/files/prime-again.sh b/linux-intro/files/prime-again.sh
new file mode 100644 (file)
index 0000000..116c1bb
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/env bash
+
+# Prints prime numbers, dividable evenly only by itself and one
+
+# Here are alternative methods
+
+N=99
+i=1; printf "%4d" $i
+i=2; printf "%4d" $i
+while [[ $i < $N ]]; do
+    i=$(( $i + 1 ))
+    if ! seq 2 $(( i - 1 )) | sed "s/^/$i%/" | bc | grep -q "^0$"; then
+        printf "%4d" $i
+    fi
+done
+printf "\n"
diff --git a/linux-intro/files/prime.c b/linux-intro/files/prime.c
new file mode 100644 (file)
index 0000000..639d4a8
--- /dev/null
@@ -0,0 +1,20 @@
+/* Prints prime numbers, dividable evenly only by itself and one */
+
+#include <stdio.h>
+
+#define N 99
+
+int main(void)
+{
+  int i, j;
+  i = 1;
+  printf ("%5d", i);
+  while (i < N)
+    {
+      i++;
+      j = 2;
+      while (i % j != 0) j++;
+      if (j == i) printf ("%5d", i);
+    }
+  printf ("\n");
+}
diff --git a/linux-intro/files/prime.sh b/linux-intro/files/prime.sh
new file mode 100644 (file)
index 0000000..2d91899
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/env bash
+
+# Prints prime numbers, dividable evenly only by itself and one
+
+N=99
+
+i=1
+printf "%5d" $i
+while [[ $i < $N ]]; do
+    i=$(( $i + 1 ))
+    j=2
+    while [[ $(( $i % $j )) != 0 ]]; do
+        j=$(( $j + 1 ))
+    done
+    if [[ $j = $i ]]; then
+        printf "%5d" $i
+    fi
+done
+printf "\n"