Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > how to run more faster?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 19th October 07:01
cokalue
External User
 
Posts: 1
Default how to run more faster?



hi guys

on 2^240 or 70! but when the number get larger,the speed becomes very
slow.
here`s my awk code:

-------------------------------------------------------------------------------------

#! /usr/bin/awk -f
# by cokalue
# run like this : echo "31 31"| awk -f cal
{
print IntegerPow($1,$2)
print fact($1)
}


function
multiply(xr,yr,x,y,a,b,lenX,lenY,lenZi,lenXY2,k,i, sum1,zt,z,c,xt,yt,result)
{
split(xr,xt,".")
split(yr,yt,".")
x=xt[1]xt[2]
y=yt[1]yt[2]
lenX=length(x)
lenY=length(y)
lenXY2=length(xt[2]yt[2])
split(x,a,"")
split(y,b,"")
for(k=2;k<=lenX+lenY;k++)
{

for(i=1;i<=k;i++)
{
zt[lenX+lenY+2-k]+=a[i]*b[k-i]
}


}

for(k=2;k<=lenX+lenY;k++)
{

split(zt[k],c,"") lenZ=length(zt[k])
for(i=lenZ;i>=1;i--) {
if(z[k+lenZ-i]+c[i]>=10)
{
z[k+lenZ-i]+=c[i]-10
z[k+lenZ-i+1]+=1
}else{
z[k+lenZ-i]+=c[i]
} } }
for(k=lenX+lenY+1;k>=2;k--)
{
if(k==1+lenXY2){result=result".";}
result=result""z[k]
}

return result

}


function IntegerPow(x,y)
{


if(y==1){
return x }
if(y>1)
{
if(y%2==1)
{
y=(y-1)/2
return
multiply(x,multiply(IntegerPow(x,y),IntegerPow(x,y )))
}
if(y%2!=1)
{
y=y/2
return multiply(IntegerPow(x,y),IntegerPow(x,y))
}
}

}

function fact(x,n,s){
s=1
for(n=1;n<=x;n++)
{
s=multiply(s,n)
}
return s
}


-----------------------------------------------------------------------------------

i use strings to instead of large numbers when calculating . maybe my
Algorithms is
not good ,i`am not a programmer . any method to run that program more
faster?
or any advices ?


Thank you ver much!


cocalue
  Reply With Quote


 


2 19th October 07:01
External User
 
Posts: 1
Default how to run more faster?



Use bc or calc instead.
--
Using OpenBSD with or without X & KDE?
http://dfeustel.home.mindspring.com
  Reply With Quote
3 19th October 07:01
juergen kahrs
External User
 
Posts: 1
Default how to run more faster?


bc really is what is used on Unix systems in this case.
bc is a standard POSIX command, so it should exist on every Unix.

http://www.opengroup.org/onlinepubs/...99/xcu/bc.html

Anyway, is he really wants to use AWK as a scripting
language, then he can try xgawk, the extended GNU Awk.
The xgawk distribution contains an interface to the
MPFR library, which can handle number with thousands
of decimal places. MPFR is really fast in calculating
with high precision.

http://www.mpfr.org/
http://sourceforge.net/projects/xmlgawk/
  Reply With Quote
4 19th October 07:01
External User
 
Posts: 1
Default how to run more faster?


Calc is much handier. Find it at http://www.isthe.com.
--
Using OpenBSD with or without X & KDE?
http://dfeustel.home.mindspring.com
  Reply With Quote
5 19th October 07:01
gazelle
External User
 
Posts: 1
Default how to run more faster?


All of which is completely OT here, of course.
And almost certainly useless to the OP, who wants to know how to make
his AWK programs more efficient.
  Reply With Quote
6 19th October 07:01
jürgen_kahrs
External User
 
Posts: 1
Default how to run more faster?


Let the OP decide what is useful to him.
Sometimes users are open to new tools.
  Reply With Quote
7 19th October 07:01
gazelle
External User
 
Posts: 1
Default how to run more faster?


But this is hardly the time, nor the place.
  Reply With Quote
8 19th October 07:02
cokalue
External User
 
Posts: 1
Default how to run more faster?


Thank you !
i know bc and calc can do arbitrary precision calculation.but i like
using awk , it is more useful than bc even it is really hard to do
arbitrary precision computing . i am not sure how to use MPFR in xgawk,
and it seems MPFR home page don`t mentioned how to do that .
I know a little c ,for me it is too hard to write a MPFR to xgawk
interface or an extension.
So I'll change my own awkprogram ^_^

regards
  Reply With Quote
9 19th October 07:02
jürgen_kahrs
External User
 
Posts: 1
Default how to run more faster?


That's true. There is currently no documentation for MPFR in xgawk.
One of the regression test scripts looks like this:

BEGIN {
if (ARGC != 2) {
print "FERMAT - factorize a number using Fermat's algorithm"
print "IN:"
print " a number as first and only command line paramater"
print "OUT:"
print " two factors"
print "CALL:"
print " gawk -f fermat.awk number"
print "EXAMPLE:"
print " gawk -f fermat.awk 65537 "
print "JK 2002-03-14"
print "JK 2006-01-27"
exit
}
MPFR_MANTISSA=200
n=ARGV[1]; ARGV[1] = ""
if (n<1) { print "number must be greater than zero\n" ; exit }
if ((n % 2) == 0) { print "number must be odd\n" ; exit }
xx = 1
yy = 1
e = mpfr_sub(yy-xx, n)
while (mpfr_zero_p(e) == 0 ) {
if (mpfr_less_p(e,0)) {
e = mpfr_add(e, yy)
yy = mpfr_add(yy, 2)
} else {
e = mpfr_sub(e, xx)
xx = mpfr_add(xx, 2)
}
}
x = mpfr_div(mpfr_sub(xx, 1), 2);
y = mpfr_div(mpfr_sub(yy, 1), 2);
q = mpfr_add(y, x);
p = mpfr_sub(y, x);
print p, q
}


You see, the mantissa is set to 200 bits, you could
set it to 10000 if needed.


OK, but if your calculations start running for hours,
remember that there is an alternative.
  Reply With Quote
Reply


Thread Tools
Display Modes




666