@: Expression syntax

@: Expression syntax

Post by Michael (Yuk- » Mon, 07 Dec 1992 06:54:19



Can anyone please tell me what's the problem with this csh script?

When I type:

factorial 3


Please respond with email.  Thanks a lot.

#factorial
set n = $argv[1]
set fact = 1
set count = $n

if ($n == 1) then
        echo 1
        exit(0)
else

        set y = `$0 $count`

endif
echo $fact

--

 
 
 

@: Expression syntax

Post by Roseb » Tue, 08 Dec 1992 01:13:11



>Can anyone please tell me what's the problem with this csh script?
>When I type: factorial 3

>Please respond with email.  Thanks a lot.

        Oh, come on.  This group is not that high in traffic.  I'll
mail you a copy of the post, but the group is worth following.  You
might pick a few neat things.

>#factorial
>set n = $argv[1]
>set fact = 1
>set count = $n
>if ($n == 1) then
>    echo 1
>    exit(0)
>else              

>        set y = `$0 $count`

>endif
>echo $fact

        Well, for one, it's horribly inefficient.  See the "why not to
program in csh faq" for reasons not to use csh for programming.  But,
when I ran your script, it worked and got the correct answer for
numbers less than 13.
        I tried versions of factorial in sh, csh, and perl and came to
the conclusion that (1) programming in csh is often not such a good
idea and (2) perl is really nice.  In turns out that it takes less
time to compute 100! in perl than 3! in your csh script!  Here's the
time data:

For 3! (the csh version is quite slow)
Xenon:~ >time cshfact 3
6
1.530u 0.470s 0:01.97 101.5% 0+199k 0+0io 0pf+0w

For 12! (all three versions are correct)
Xenon:~ >time cshfact 12
479001600
6.340u 1.830s 0:08.04 101.6% 0+194k 0+0io 0pf+0w
Xenon:~ >time shfact 12
479001600
0.400u 1.440s 0:01.65 111.5% 0+43k 0+0io 0pf+0w
Xenon:~ >time perlfact 12
479001600
0.040u 0.140s 0:00.15 120.0% 0+154k 0+0io 0pf+0w

For 14! (sh and csh versions get the wrong answer)
Xenon:~ >time cshfact 14
1278945280
7.520u 2.840s 0:10.20 101.5% 0+195k 0+0io 0pf+0w
Xenon:~ >time shfact 14
1278945280
0.500u 1.720s 0:01.97 112.6% 0+44k 0+0io 0pf+0w
Xenon:~ >time perlfact 14
87178291200
0.030u 0.150s 0:00.15 120.0% 0+155k 0+0io 0pf+0w

For 15! (csh version blows up)
Xenon:~ >time cshfact 15
Fork failed.
0
7.320u 2.330s 0:09.52 101.3% 0+194k 0+2io 0pf+0w
Xenon:~ >time shfact 15
2004310016
0.440u 2.070s 0:02.20 114.0% 0+44k 0+0io 0pf+0w
Xenon:~ >time perlfact 15
1307674368000
0.060u 0.120s 0:00.15 120.0% 0+155k 0+0io 0pf+0w

For 100!
Xenon:~ >time perlfact 100
9.3326215443944102188e+157
0.110u 0.160s 0:00.24 112.5% 0+207k 0+0io 0pf+0w

Here's the scripts I used (notice which is the shortest :-)

#!/usr/bin/perl
sub fact {

        ($num lt 1) ? 1 : $num * &fact($num - 1);

Quote:}

print &fact($ARGV[0]), "\n";

#!/bin/csh
set num = $argv[1]
if ($num <= 1) then
        echo 1


        echo $fact
endif

#!/bin/sh
fact () {
        if [ $1 -le 1 ]
                then    num=1
                else    fact `expr $1 - 1`
                        num=`expr $1 \* $num`
        fi

Quote:}

fact $1
echo $num

                                Yours,
                                        Larry Schwimmer


 
 
 

@: Expression syntax

Post by Roseb » Wed, 09 Dec 1992 12:29:03



>>Can anyone please tell me what's the problem with this csh script?
>>When I type: factorial 3



Quote:>    Well, for one, it's horribly inefficient.  See the "why not to
>program in csh faq" for reasons not to use csh for programming.  But,
>when I ran your script, it worked and got the correct answer for
>numbers less than 13.
>    I tried versions of factorial in sh, csh, and perl and came to
>the conclusion that (1) programming in csh is often not such a good
>idea and (2) perl is really nice.  In turns out that it takes less
>time to compute 100! in perl than 3! in your csh script!  Here's the
>time data:

[data deleted]

Quote:>Here's the scripts I used (notice which is the shortest :-)
>#!/usr/bin/perl
>#!/bin/csh
>#!/bin/sh

[all but paths deleted]

        It has been pointed out that #!/bin/csh -f (fast start --
don't read the .cshrc) runs quite a bit faster.  Still not as fast as
the sh and perl versions, but more like a peppy turtle than the snail
my script was.  And, yes, an iterative solution would be faster still,
but the who point of the original question was doing recursive
functions, which csh doesn't do very elegantly.
        Of course, it has yet to be noted on this group, which is why
I am posting :-)  I held off on making my post for a few days.  When I
saw that no one posted an answer, I thought I'd take a look at it.
This group's FAQ seems to favor email over posting, but the group is
not of much value if answers aren't posted as well.  Even tainted ones
like mine.  We all learn something that way (-:

                                Yours,
                                        Larry Schwimmer

 
 
 

1. Regular Expression Syntax Limitation?

Hi all,

I've been trying to use grep to filter a file whenever it says Copyright
[some name other than Microsoft].

I thought this expression syntax might be appropriate:

'opyright.*[^M][^i][^c][^r][^o][^s][^o][^f][^t]'

However this still finds strings that are Copyright ... Microsoft (e.g.
Copyright (c) 1997 - 1999 Microsoft Corporation). This makes sense
because, for example, " (c) 1997" doesn't match "Microsoft".

Any advice about how to approach this situation differently?

Many thanks,
Adam

2. Hercules Dynamite Power - 1280x1024@75Hz - How ?

3. Help with samples of regular expressions syntax please

4. X-windows graphics algorithms

5. tcsh Controlling Expressions Syntax

6. Cirrrus Chipsets and AST P40's

7. if :Expression Syntax problem at login

8. compiling apapche1.0.3 under Slackware 3.0 (linux1.2.13ELF)

9. if expression syntax error

10. Why does 'if ($TTY == "not a tty") then' give me an "Expression syntax" error?

11. Need to know correct syntax for conditional expression

12. inline assembler error - porting Intel syntax to AT&T syntax

13. intel Syntax -> AT&T Syntax