Skip to content

Instantly share code, notes, and snippets.

@tatarurzvn
Created November 1, 2017 21:06
Show Gist options
  • Save tatarurzvn/232537d461a22cf1449b9c3b8d6eaed5 to your computer and use it in GitHub Desktop.
Save tatarurzvn/232537d461a22cf1449b9c3b8d6eaed5 to your computer and use it in GitHub Desktop.
Shell(zsh) exercises: print n fibos, is prime and print n primes; CN Homework
#!/bin/zsh
echo "Hey $USER, please enter a number: "
read NBR
CHECK=2
IS_PRIME=1
while [ $CHECK -lt `expr $NBR / 2` ]
do
if [ `expr $NBR % $CHECK` -eq 0 ]
then
IS_PRIME=0
break
fi
CHECK=$((CHECK+1))
done
if [ $IS_PRIME -eq 1 ]
then
echo "Your number is prime!"
else
echo "Your number is not prime! :("
fi
#!/bin/zsh
echo "How many numbers do you want me to print? ans: "; read n
if [ $n -lt 1 ]
then
echo "You must be retarded"
exit
fi
last1=1
last2=1
if [ $n -eq 1 ]
then
echo "1"
exit
fi
echo "1\n1"
for i in `seq 3 $n`
do
last2=$((last2+last1))
last1=$((last2-last1))
echo $last2
done
#!bin/zsh
echo "How many primes do you want me to print?"; read nbr_primes
if [ $nbr_primes -lt 1 ]
then
echo "Wrong number mate :("
exit
fi
prime_nbr=2
for i in `seq 1 $nbr_primes`
do
while [ true ]
do
checker=2
prime=1
while [ $checker -le `expr $prime_nbr / 2` ]
do
if [ `expr $prime_nbr % $checker` -eq 0 ]
then
prime=0
break
fi
checker=$((checker+1))
done
if [ $prime -eq 1 ]
then
echo $i": "$prime_nbr
break
fi
prime_nbr=$((prime_nbr+1))
done
prime_nbr=$((prime_nbr+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment