Qbasicnews.com
November 13, 2019, 07:09:32 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2] 3 4
  Print  
Author Topic: Statistical Challenge  (Read 18996 times)
toonski84
__/--\__
*****
Posts: 2567



« Reply #15 on: May 01, 2003, 06:23:58 PM »

always go with formulas when it's faster.  however, the formula for factorals is just *$%&*ed up.

as for summation, you try too hard:

Code:
function summation (n)
  summation = n * (n + 1) \ 2
end function


Wink
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #16 on: May 02, 2003, 03:43:23 AM »

Neo: I already said you could not use your library in another thread (I forget which one though  :wink: ). No one is to use a lib, but you veterans can pull all that hardware stuff if you want (I don't know how it will help...)

Lanzaa: WTF??? Factorials is 1*2*3*..*n for number n. What's all this addition?

So far there is 0 entries. C'mon you guys, especially agamemnus, all you have to do is account for special cases and you have a proper entry!
Logged

toonski84
__/--\__
*****
Posts: 2567



« Reply #17 on: May 02, 2003, 08:39:55 AM »

without using neo's lib it works the same way

Code:
function factoral& (number&)
  y& = 1
  for x& = 2 to number&
    y& = y& * x&
  next x&

  factoral& = y&
end function


but it can't go that high, only to about 2 or 3 million, the limit of a long integer.  you could always use double precision, but that would get inaccurate.  and i doubt lanzaa didnt know what a factoral was, that's why he asked a second question ("how about...")
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #18 on: May 02, 2003, 08:23:16 PM »

That's also wrong... please read the above posts. This is easy guys, surely someone can do it correctly... or do I have to submit an entry myself and ruin it for you all?
Logged

Neo
Na_th_an
*****
Posts: 2150



« Reply #19 on: May 03, 2003, 08:36:18 AM »

OK here it is:

Code:
'My Library
'
' BIGINT
'
' A small library having support for so-called 'big integers',
' which are unsigned data types consisting of a maximum of
' 262144 bits = 32768 bytes.
' Compare this with a normal integer: 16 bits = 2 bytes!
'
' Actually, this version of BIGINT has support for:
' - String integers (max value = 32768x a 9)

' Have fun!
'
' -Neo Deus Ex Machina
'
'       X
'      XXX
'     X   X
'    XXX XXX
'
' HAR-SoftWare
'
' PS: Due to the fact that this is a pure-qb big integer
' library, I think you will get out of string space when
' using large big integers (like 10000!)

DECLARE FUNCTION SI.Add$(StrInt AS STRING, StrInt2 AS STRING)
DECLARE FUNCTION SI.Multiply$(StrInt AS STRING, StrInt2 AS
STRING)
DECLARE FUNCTION Fac$(n AS INTEGER)
CONST FALSE = 0
CONST TRUE = NOT FALSE
CONST LN.E = 2.718281828#
DEFINT A-Z
'$DYNAMIC
'$END_META

FUNCTION SI.Add$(StrInt AS STRING, StrInt2 AS STRING)
   '
   ' This functions adds two String ints to eachother and
   ' returns the answer in a string int
   '

   'declare stuff
        DIM len1 AS LONG, len2 AS LONG, maxlen AS LONG
        DIM StrAns AS STRING, c AS LONG, char1 AS STRING, char2 AS STRING
        len1 = LEN(StrInt)
        len2 = LEN(StrInt2)

        'pick out maximum length of the strings
        IF len1<len2 THEN
           maxlen = len2
           StrAns = SPACE$(len2 + 1)
        ELSE
           maxlen = len1
           StrAns = SPACE$(len1 + 1)
        END IF

   'calculate StrInt + StrInt2
   over% = 0
   ' PRINT maxlen
        FOR c = 1 TO maxlen
           IF c <= LEN(StrInt) THEN char1 = MID$(StrInt, len1 - c + 1, 1) ELSE char1 = "0"
      IF c <= LEN(StrInt2) THEN char2 = MID$(StrInt2, len2 - c + 1, 1) ELSE char2 = "0"
      val1 = VAL(char1)
      val2 = VAL(char2)
      ' print val1,val2
      ans$ = LTRIM$(STR$(val1 + val2 + over%))
      MID$(StrAns, LEN(StrAns) - c + 1, 1) = RIGHT$(ans$, 1)
      IF LEN(ans$) > 1 THEN
         over% = VAL(LEFT$(ans$, 1))
      ELSE
         over% = 0
      END IF
        NEXT c
        StrAns = LTRIM$(StrAns)
        IF over% > 0 THEN StrAns = LTRIM$(STR$(over%)) + StrAns
        SI.Add$ = StrAns
END FUNCTION

FUNCTION SI.Multiply$(StrInt AS STRING, StrInt2 AS STRING)
   '
   ' This functions multiplies two String ints to eachother and
   ' returns the answer in a string int
   '

   'declare stuff
        DIM len1 AS LONG, len2 AS LONG, maxlen AS LONG
        DIM StrAns AS STRING, c AS LONG, char1 AS STRING, char2 AS STRING
        DIM TempAns AS STRING
        len1 = LEN(StrInt)
        len2 = LEN(StrInt2)

        'pick out maximum length of the strings
        IF len1<len2 THEN
           maxlen = len2
           ' StrAns = SPACE$(len2 + 1)
        ELSE
           maxlen = len1
           ' StrAns = SPACE$(len1 + 1)
        END IF
        StrAns = "0"

   'calculate StrInt + StrInt2
   over% = 0
   ' PRINT maxlen
        FOR d = 1 TO len2
           IF d>1 THEN
              TempAns=STRING$(d-1,"0")
           ELSE
              TempAns=""
           END IF
           over% = 0
           char2 = MID$(StrInt2, len2 - d + 1, 1)
      val2 = VAL(char2)
           FOR c = 1 TO len1
              char1 = MID$(StrInt, len1 - c + 1, 1)
              'print char1;" ";char2
         val1 = VAL(char1)
                        ans$ = LTRIM$(STR$(val1 * val2 + over%))
                        'print ans$
         TempAns = RIGHT$(ans$, 1) + TempAns
         'print TempAns
         IF LEN(ans$) > 1 THEN
            over% = VAL(LEFT$(ans$, 1))
         ELSE
            over% = 0
         END IF
      NEXT c
      IF over% > 0 THEN TempAns = LTRIM$(STR$(over%)) + TempAns
      StrAns = SI.Add$(StrAns, TempAns)
      'PRINT StrAns
        NEXT d
        StrAns = LTRIM$(StrAns)
        SI.Multiply$ = StrAns
END FUNCTION

'---- Here is the function that can calculate factorials
FUNCTION Fac$(n AS INTEGER)
   IF n <= 1 THEN Fac$ = "1": EXIT FUNCTION
   ans$ = "1"
   FOR C% = 2 TO n
      ans$ = SI.Multiply$(ans$, LTRIM$(STR$(C%^)))
   NEXT C%
   Fac$ = ans$
END FUNCTION


Now happy? Wink
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #20 on: May 03, 2003, 09:30:16 AM »

Code:
FUNCTION Factorial& (n&)
   IF n& < 0 THEN
      ERROR 18         ' Function not defined.
   ELSEIF n& = 0 THEN
      Factorial& = 1
   ELSE
      Factorial& = Factorial&(n& - 1) * n&
   END IF
END FUNCTION
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #21 on: May 03, 2003, 10:04:47 AM »

0! != 1!
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #22 on: May 03, 2003, 11:26:29 AM »

Of course!!! 0! = 1, according to factorial's definition

See it by yourself:

http://mathworld.wolfram.com/Factorial.html
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #23 on: May 03, 2003, 05:24:15 PM »

Code:
CLS
DEFDBL F, I
INPUT n

f = 1
 FOR I = 1 TO n
  f = I * f
 NEXT I

z$ = LEFT$(LTRIM$(STR$(n)), 1)

FOR c = 0 TO 9
c$ = LTRIM$(STR$(c))
IF z$ = c$ THEN EXIT FOR
IF c = 9 THEN PRINT "Null": END
NEXT c

PRINT f


It goes up to 18!

Is it ok?

Eidt: If you can read Scientific Notation... It goes up to 170!

Edit: I made a mistake... Try it again...
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #24 on: May 04, 2003, 04:09:27 AM »

whitetiger: you made another mistake, but not in the code...

Na_th_an's looks good, but I'll check it when I boot into my windows partition.

Whitetiger's looks too complicated for me to decide exactly what it does but I will check that too.

Nath: I'm wondering if instead of returning an error (and therefore stuffing up the whole prog) it should just return 0 or something... keeping in mind my next mini-challenge in this thread is going to be the combination and permutation forumla...
Logged

whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #25 on: May 04, 2003, 10:26:31 AM »

whats did i do wrong
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #26 on: May 04, 2003, 10:43:54 AM »

Quote from: "oracle"
Nath: I'm wondering if instead of returning an error (and therefore stuffing up the whole prog) it should just return 0 or something


Definitely not zero. The factorial of a negative number is NOT DEFINED, so we better find a representation for "NOT A NUMBER" (NAN). As QB doesn't suport NAN natively, I inserted that error. But we could set NAN as the maximum or the minimum long integer in range, and trap the exit so if it is that value, then it is considered NAN. But that would involve making a whole math library LOL Cheesy.

And be sure my implementation works. It is just a QB translation of the recursive definition of factorial (as in maths):



Compare that formula with my code Wink

Yo can do it iteratively also:

Code:
FUNCTION IterativeFactorial&(n&)
   IF n&<0 THEN ERROR 18
   fact&=1
   FOR i&=1 TO n&
      fact& = fact& * i&
   NEXT i&
   IterativeFactorial& = fact&
END FUNCTION
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #27 on: May 04, 2003, 10:09:18 PM »

I'll check your new method when I get home from school. I can't really read the formula, but I'll try later, but for the moment you are de-facto winner.

whitetiger: eidt

Everyone who sees this before I get home: Take na_th_an's code and make both a permutation and a combination function. Go searching on google if you don't know the formula.

----
edit
----

I'm home. na_th_an's factorial program is the current winner, but it can be improved so the contest is not over. Hurry up, guys!

ps: nath: your prog loses out because it can only do up to 12! (hint, hint!)
Logged

na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #28 on: May 05, 2003, 07:57:49 AM »

Quote from: "oracle"
ps: nath: your prog loses out because it can only do up to 12! (hint, hint!)


If you want more, just use Neo's long int library. 12! is your top in integer math at least you do some tricks.

You can use the DOUBLE data type, this way you can calculate up to 170!, but you are losing lots of ciphers and precission (QB just callculates a power so it fills with trailing zeroes in floating point).

Code:
FUNCTION fact# (n#)
   f# = 1
   FOR i# = 1 TO n#
      f# = f# * i#
   NEXT i#
   fact# = f#
END FUNCTION


I'd stick to the integer sollution. 12! is not a very big number, but it is enough for most caluclations. If you need bigger numbers, you better use neo's code.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Neo
Na_th_an
*****
Posts: 2150



« Reply #29 on: May 06, 2003, 05:11:22 AM »

Wink lol
Logged
Pages: 1 [2] 3 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!