home *** CD-ROM | disk | FTP | other *** search
- /*
- Listing 21.11 Mathematics
- Author: Joe Booth
- Excerpted from "Clipper 5: A Developer's Guide"
- Copyright (c) 1991 M&T Books
- 501 Galveston Drive
- Redwood City, CA 94063-4728
- (415) 366-3600
- */
-
- function Bin2dec(cBinary)
- LOCAL nDec:=0,jj
- for jj=1 to len(cBinary)
- nDec:=nDec+nDec+val(substr(cBinary,jj,1))
- next
- return nDec
-
-
- function Dec2bin(nDecimal)
- LOCAL nTemp:=nDecimal,cBinary:=""
- LOCAL nDiv :=int(nTemp/2),nRem:=0
- while nDiv <> 0
- nRem := nTemp-(2*nDiv)
- cBinary := substr("01",nRem+1,1)+cBinary
- nTemp := nDiv
- nDiv := int(nTemp/2)
- enddo
- return cBinary
-
-
- function Hex2dec(cHex)
- LOCAL nDec:=0,jj
- for jj=1 to len(cHex)
- nDec=nDec*16 + at(substr(cHex,jj,1),"0123456789ABCDEF")
- next
- return nDec
-
-
- function Dec2hex(nDec)
- LOCAL nTemp:=nDec,cHex:=""
- LOCAL nDiv :=int(nTemp/16),nRem:=0
- while nDiv <> 0
- nRem := nTemp-(16*nDiv)
- cHex := substr("0123456789ABCDEF",nRem+1,1)+cHex
- nTemp := nDiv
- nDiv := int(nTemp/16)
- enddo
- return cHex
-
-
- function Gcd(nX,nY)
- LOCAL rmd:=1,quot:=0
- while rmd <> 0
- quot:=int(nX/nY) ; rmd := nX - (nY*quot)
- if rmd <> 0
- nX:=nY ; nY := rmd
- endif
- enddo
- return nY
-
-
- function Isprime(nX)
- LOCAL jj
- if nX = 1
- return .F.
- elseif nX < 4
- return .T.
- endif
- if nX = int(nX/2)*2
- return .F.
- endif
- for jj=3 to int(sqrt(nX)) step 2
- if nX=int(nX/jj)*jj
- return .F.
- endif
- next
- return .T.
-
-
- function lcm(nX,nY)
- return nX*nY/gcd(nX,nY)
-
- // end of file CHP2111.PRG
-