Skip to content

Commit

Permalink
Pattern49 in Bash
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0625 committed Oct 13, 2023
1 parent c146365 commit 482a01f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 53 deletions.
53 changes: 0 additions & 53 deletions PATTERNS/Pattern49/Pattern49.cpp

This file was deleted.

50 changes: 50 additions & 0 deletions PATTERNS/Pattern49/Pattern49.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

isPrime() {
num=$1
if [ $num -le 1 ]; then
echo 0
else
for ((i=2; i*i<=num; i++)); do
if [ $((num % i)) -eq 0 ]; then
echo 0
return
fi
done
echo 1
fi
}

printLineOfPrimes() {
offset=$1
lineDigits=$2
max=$3

if [ $lineDigits -ge $max ]; then
return
fi

primesInThisLine=0
space=$((max - lineDigits))

for ((i=0; i<space; i++)); do
echo -n " "
done

while [ $primesInThisLine -lt $lineDigits ]; do
if [ $(isPrime $offset) -eq 1 ]; then
echo -n "$offset "
((primesInThisLine++))
fi

((offset++))
done

echo
printLineOfPrimes $offset $((lineDigits+1)) $max
}

echo -n "Enter the number of lines N : "
read n

printLineOfPrimes 1 1 $((n+1))

0 comments on commit 482a01f

Please sign in to comment.