A perfect number shell program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers A perfect number shell program
# 8  
Old 10-09-2009
Ok.. thanks Decent and Methyl for letting me know about backticks.

Here's my updated program:
Code:
echo Enter a number
read no
i=1
ans=0
while [ "$i" -le "$(expr $no / 2)" ]
   do
       if [ `expr $no % $i` -eq 0 ]
       then
          ans=`expr $ans + $i`
       fi
       i=`expr $i + 1`
done
if [ $no -eq $ans ]
then
echo $no is perfect
else
echo $no is NOT perfect
fi

I have inserted the double quotes here in this fifth line:
Code:
while [ "$i" -le "$(expr $no / 2)" ]

Again the line 5 error!!
[: : integer expression expected

Now what? Smilie
# 9  
Old 10-09-2009
'-le' (as well as -gt/-ge/-eq/-ne/-lt) is for testing integers, while quotes (single, double) force string interpretation. Loose the quotes, and it should work as expected.
# 10  
Old 10-09-2009
When I loose the quotes..
The terminal says, Unary operator expected.
# 11  
Old 10-09-2009
Please state what Operating System and shell you are using. Arithmetic varies from shell to shell.

Also, there are too many errors in the script to work out whether your idea of a Perfect Number is the same a the strict definition in mathematics.
Can you explain what you are trying to do?
# 12  
Old 10-09-2009
corrected version:

Code:
#  cat perfect.sh
#!/bin/sh
echo Enter a number
read no
i=1
ans=0
while [ $i -le `expr $no / 2` ]
   do
       if [ `expr $no % $i` -eq 0 ]
       then
          ans=`expr $ans + $i`
       fi
   i=`expr $i + 1`
  done
if [ $no -eq $ans ]
then
echo $no is perfect
else
echo $no is NOT perfect
fi

#  ./perfect.sh
Enter a number
6
6 is perfect

#  ./perfect.sh
Enter a number
28
28 is perfect

#  ./perfect.sh 
Enter a number
8
8 is NOT perfect

Alternate version:

Code:
#  cat perfect.awk
nawk 'BEGIN {
 t=0
 printf("Enter a Number (or q to quit): ")
}

$0 ~ /^(quit|[qQ]|exit|[Xx])$/ { exit }

{
for (i=1;i<=$1/2;i++) { if (int($1/i)*i==$1){t+=i}}
if (t==$1){print $1" is perfect"} else {print $1" is not perfect"}
}

{
 t=0
 printf("Enter another Number (q to quit): ")
}' -

#  ./perfect.awk
Enter a Number (or q to quit): 1
1 is not perfect
Enter another Number (q to quit): 6
6 is perfect
Enter another Number (q to quit): 28
28 is perfect
Enter another Number (q to quit): 7
7 is not perfect
Enter another Number (q to quit): q

HTH

Last edited by Tytalus; 10-09-2009 at 07:45 AM.. Reason: removing my prompt from output :-)
# 13  
Old 10-09-2009
Well done Tytalus. ... methyl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

2. Shell Programming and Scripting

count number of entries perl program or Unix script

Hi I have a file with number of entries name 1 123 name 1 345 name 1 65346 name2 3243 name2 24234 name 2 234234 so on ......... how to count total number of entries for name 1 and name2...and so on Please guide. (1 Reply)
Discussion started by: manigrover
1 Replies

3. UNIX for Dummies Questions & Answers

Can you perfect my sed ?

I want to print only the lines that meet the criteria : "worde:" and "wordo;" I got this far: sed -n '/\(*\)\1e:\1o;/p;' But it doesn't quite work. Can someone please perfect it and tell me exactly how its a fixed version/what was wrong with mine? Thanks heaps, (1 Reply)
Discussion started by: maximus73
1 Replies

4. Solaris

Resolving port number to program name

I was just checking to see if anyone had a script that would allow me to go from port number to program name. I tried to create my own script but it looks like it only works for IPv4 sockets and it looks like daemons such as sshd return as AF_INET6 (in pfiles) for some reason. I can fix my script... (0 Replies)
Discussion started by: thmnetwork
0 Replies

5. Shell Programming and Scripting

ksh program that finds the lowest number in a .txt file

i am having a problem finding the lowest number after punching in a bunch of numbers in the .txt file but its probably the way i have the code set up. help please! (4 Replies)
Discussion started by: tinsteer
4 Replies

6. AIX

I want the perfect user-interface

I've got an aix-box somewhere on the network and a PC on my desk. Nothing fancy so far. The PC is made dual-boot: - windowsXP with putty & winSCP or - slackware 13 with xfce4 installed. The aix-box runs DB2 v8.2 and I've installed db2top to monitor the database. db2top is a character... (0 Replies)
Discussion started by: dr_te_z
0 Replies

7. UNIX for Dummies Questions & Answers

Script producing error, Program to calculate maximum number

Hi folks, Here i have written a shell script to calculate a maximum number from 10 numbers entered on command line. max=0 echo Enter 10 numbers , one at a time for i in 1 2 3 4 5 6 7 8 9 10 do read n max=`expr $max + $n` if --- At this last step there is some problem, it gives error... (5 Replies)
Discussion started by: rits
5 Replies

8. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies

9. Shell Programming and Scripting

Prime Number Program (Fun)

Hi, I was just wondering if anyone has, or knows where to download a prime number finder program. I would like a fairly simple bash program, and also I would like one that could take advantage of multiple processors. I have 500 cores I can use, and would like to take advantage of them using a... (2 Replies)
Discussion started by: Kweekwom
2 Replies

10. Shell Programming and Scripting

Write a shell program to find the sum of alternate digits in a given 5-digit number

Hi Can any one please post the answer for the above program.................. (4 Replies)
Discussion started by: banta
4 Replies
Login or Register to Ask a Question