Problem in shell code to find happy prime number - shell

This is my code to find happy prime number, I don't know why it is giving error.
#!/bin/bash
happy(){
rem = $0
sum = $0
while [ $result -gt 0 ];
do
rem = $((result%10))
p = $((rem*rem))
sum = $((sum + p))
result = $((result/10))
done
return $sum
}
echo "Enter a number"
read num
for (( i=1; i<101; i++ ))
do
result=$i
while [ $result -ne 0 && $result -ne 4 ];
do
happy $result
done
if [ $?==1 ]
then echo "happy number"
else
echo "not a happy number"
fi
done

I see lots of syntax errors and some logical errors in your script.
This should be a fixed version (works at least for 13 and 4 :))
Errors I've found:
rem = $0 and similar: the spaces around assignments are not allowed in bash,
rem = $0: you assign a value that is never used,
rem = $0 and sum = $0 the first argument passed to the function is $1 not $0,
the input number is assigned to num but never used,
the exit condition from the for and while loops is broken,
...
#!/bin/bash
happy()
{
result=$1
sum=0
while [ $result -gt 0 ]; do
rem=$(( result % 10 ))
p=$(( rem * rem ))
sum=$(( sum + $p ))
result=$(( result / 10 ))
done
echo "$sum"
}
echo "Enter a number"
read num
result=$num
for (( i=1; i<101; i++ )) do
result=$(happy $result)
if [ $result == 1 ]; then
echo "$num is a happy number"
exit
fi
done
echo "$num is not a happy number"

Related

How to break while loop after 5 attempts?

How to keep track of how many guesses the user makes and print a message that tells them they lose? Exit after 5 guesses.
num=$(( $RANDOM % 100 + 1 ))
while [ "$input" -ne "$num" ]; do
if [ "$input" -gt "$num" ]; then
echo "The number is too high."
read input
elif [ "$input" -lt "$num" ]; then
echo "The number is too low."
read input
fi
done
echo "Great, you picked the right number."
Would you please try the following:
#!/bin/bash
num=$(( $RANDOM % 100 + 1 ))
for (( i = 0; i < 5; i++ )); do
read -p "Enter a number: " input
if (( input == num )); then
echo "Great, you picked the right number."
break
elif (( input > num )); then
echo "The number is too high."
elif (( input < num )); then
echo "The number is too low."
fi
done
BTW I tested the script by modifying the random number generator to $RANDOM % 10 + 1 but still not easy :).

Write a function in shell script to check if the two numbers are Palindromes

I'm writing a function in shell script to check if the two numbers are Palindromes but I am getting an error, It is showing error in line 18 command not found. Please help me how can I remove this error.
#!/bin/bash
echo "Enter two number:"
read a
read b
for num in $a $b;
do
x=$x$sep$num
sep=" "
done
y=$x
num1=$a
num2=$b
rem=""
rev=0
for word in $y;
do
checkPalindrome $word
if [ $? -eq 0 ]
then
echo "$word is palindrome"
fi
done
checkPalindrome() {
local s=$1
for i in $s ;
do
while [ $i -gt 0]
do
rem=$(($i%10));
rev=$(($rev*10+$rem));
i=$(($i / 10));
done
done
if [[ $rev -eq $num1 && $rev -eq $num2 ]]
then
return 0;
else
return 1;
fi
}
You need to provide your checkPalindrome() definition before you use it, as below:
#!/bin/bash
checkPalindrome() {
local s=$1
for i in $s
do
while [ "$i" -gt 0 ]
do
rem=$((i%10))
rev=$((rev*10+rem))
i=$((i / 10))
done
done
if [[ $rev -eq $num1 && $rev -eq $num2 ]]
then
return 0
else
return 1
fi
}
echo "Enter two number:"
read -r a
read -r b
for num in $a $b
do
x="$x$sep$num"
sep=" "
done
y="$x"
num1="$a"
num2="$b"
rem=""
rev=0
for word in $y;
do
if checkPalindrome "$word"
then
echo "$word is palindrome"
fi
done
You could consider the input as string (you don't have to restrict it to be an integer)
#!/bin/bash
is_palindrome() {
local arg=$1 i j
for ((i = 0, j = ${#arg} - 1; i < j; ++i, --j)); do
[[ ${arg:i:1} = "${arg:j:1}" ]] || return
done
}
read -r -p 'Enter two words: ' a b
for word in $a $b; do
is_palindrome "$word" && echo "$word is palindrome"
done

how to print prime numbers using for loop in shell script

I want to print prime numbers using for loop in shell script. Please provide some suggestions.
actually I could able to print odd numbers from range of 1 to 100, but now I am unable to print prime numbers. Provide some script to print prime numbers
# cat prime.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
output=$(( $i % 2))
if [ $output -ne 0 ]
then
echo "We got odd numbers: $i"
fi
done
The below code returns the list of prime numbers within a specified range.
the below command returns the prime numbers between 10 to 30
./filename.sh 10 30
#!/bin/bash
# Check if a number is prime
# function to return whether prime or not
check_prime() {
current_number=$1
flag=0
i=2
while test $i -le `expr $current_number / 2`
do
if test `expr $current_number % $i` -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then echo $current_number
fi
}
# Assign From and to number
from_number=$1
to_number=$2
for (( number=$from_number; number<=$to_number; number++ ))
do
check_prime $number
done
This code will Print all prime numbers upto given number & also print count of it
hope it helps !!
#!/bin/bash
echo "enter a number upto which you want the prime numbers"
read num
count=0
for (( n=2; n<=$num; n++ ))
do
t=1
if [ $n -lt 2 ]
then echo "Please give other numbers than 0 and 1"
else
#echo $n
for (( i=2; i<$n; i++ ))
do
#echo $n
if (($n%i==0))
then
#echo "$n is not prime number"
t=0
break;
fi
done
#echo $t
if [ $t == 1 ]
then
echo "$n is a prime number"
count=`expr $count + 1`
fi
fi
done
echo "Total prime numbers upto $num are $count "

Bash script can't figure out how to concatenating white spaces

I want to center a word in X spaces so I wrote this function:
function center {
str=$1
M=$2
N=${#str}
if (( N >= M )); then
echo $str
exit
fi
P=$((M-N))
HP=$((P/2))
left=""
right=""
for i in [1..HP]; do :
left=" $left"
right=" $right"
done
if (( (P-2*HP) == 1 )); then
right=" "$right""
fi
echo "HP is $HP"
echo "Right is |"${right}"|"
echo "Left is |$left|"
echo "str is |$str|"
res=$right$str$left
echo "$res"
}
Problem is not matter what I do can't get right or left to hold on to more than one whitespace. I have tried the suggestions on other answers but I can't seem to make them work. Please help.
Try this (after some variable quoting and some other fixes):
function center {
str=$1
M=$2
N=${#str}
if (( N >= M )); then
echo "$str"
exit
fi
(( P=M-N ))
(( HP=P/2 ))
left=$(printf '%*s' "$HP" "")
right=$left
(( (P-2*HP) == 1 )) && right=" "$right""
echo "HP is $HP"
echo "Right is |${right}|"
echo "Left is |$left|"
echo "str is |$str|"
res="$right$str$left"
echo "$res"
}
The for i in [1..HP] can not work.
A printf '%*s' "$HP" "" is a more idiomatic way to get $HP spaces.
Is better to build one variable $left and copy it to $right than to build the two.

Prime Number Script Checker

I am new to scripting. The script below checks if a number inputted is a prime number or divisible by 2. Why is the third line from the bottom needed (i=expr $i + 1). I commented that line out to see what the script would do and it hangs the script up. Please advise.
#! /bin/bash
echo -n "Enter a number: "
read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "
If you don't increment i, then the test [ $i -lt $num ] will never be false, assuming i starts out less than num.

Resources