From c146365690b94eb4dce5de422cdfc138d8e87c41 Mon Sep 17 00:00:00 2001 From: Sukhbir Singh Date: Mon, 9 Oct 2023 14:45:56 +0530 Subject: [PATCH 1/2] Pattern49 in cpp --- PATTERNS/Pattern49/Pattern49.cpp | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 PATTERNS/Pattern49/Pattern49.cpp diff --git a/PATTERNS/Pattern49/Pattern49.cpp b/PATTERNS/Pattern49/Pattern49.cpp new file mode 100644 index 0000000..493387b --- /dev/null +++ b/PATTERNS/Pattern49/Pattern49.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + +int isPrime(int num) { + if (num <= 1) { + return 0; + } + + for (int i = 2; i * i <= num; i++) { + if (num % i == 0) { + return 0; + } + } + + return 1; +} + +void printLineOfPrimes(int offset, int lineDigits, int max) { + if (lineDigits >= max) { + return; + } + + int primesInThisLine = 0; + + int space = max - lineDigits; + + for(int i = 0; i < space ; i++){ + cout << " "; + } + + while (primesInThisLine < lineDigits) { + if (isPrime(offset)) { + cout << offset << " "; + primesInThisLine++; + } + + offset++; + } + + cout << endl; + printLineOfPrimes(offset, ++lineDigits, max); +} + +int main() { + int n; + printf("Enter the number of lines N : "); + cin >> n; + + printLineOfPrimes(1, 1, n + 1); + + return 0; +} \ No newline at end of file From 482a01f0d4586ac188ffda0e4dcfb80b8ea7ed43 Mon Sep 17 00:00:00 2001 From: Sukhbir Singh Date: Fri, 13 Oct 2023 20:27:22 +0530 Subject: [PATCH 2/2] Pattern49 in Bash --- PATTERNS/Pattern49/Pattern49.cpp | 53 -------------------------------- PATTERNS/Pattern49/Pattern49.sh | 50 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 53 deletions(-) delete mode 100644 PATTERNS/Pattern49/Pattern49.cpp create mode 100644 PATTERNS/Pattern49/Pattern49.sh diff --git a/PATTERNS/Pattern49/Pattern49.cpp b/PATTERNS/Pattern49/Pattern49.cpp deleted file mode 100644 index 493387b..0000000 --- a/PATTERNS/Pattern49/Pattern49.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -using namespace std; - -int isPrime(int num) { - if (num <= 1) { - return 0; - } - - for (int i = 2; i * i <= num; i++) { - if (num % i == 0) { - return 0; - } - } - - return 1; -} - -void printLineOfPrimes(int offset, int lineDigits, int max) { - if (lineDigits >= max) { - return; - } - - int primesInThisLine = 0; - - int space = max - lineDigits; - - for(int i = 0; i < space ; i++){ - cout << " "; - } - - while (primesInThisLine < lineDigits) { - if (isPrime(offset)) { - cout << offset << " "; - primesInThisLine++; - } - - offset++; - } - - cout << endl; - printLineOfPrimes(offset, ++lineDigits, max); -} - -int main() { - int n; - printf("Enter the number of lines N : "); - cin >> n; - - printLineOfPrimes(1, 1, n + 1); - - return 0; -} \ No newline at end of file diff --git a/PATTERNS/Pattern49/Pattern49.sh b/PATTERNS/Pattern49/Pattern49.sh new file mode 100644 index 0000000..3e0bb5c --- /dev/null +++ b/PATTERNS/Pattern49/Pattern49.sh @@ -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