mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2009
Posts: 3
D
Self-satisified door
OP Offline
Self-satisified door
D
Joined: Dec 2009
Posts: 3
I'm trying to write a small factoring script, but for some reason the while loop isn't reading the variables correctly. The variables are definitely storing the correct information they just aren't being passed correctly it seems:

alias factorial {
set %factor.input $1-
set %factor.loop 0
set %factors.count 1
if ($calc(%factor.input != 0)) {
%input.str.len = %factor.input
goto numbers
}
echo -a 4letters
%input.str.len = $len(%factor.input)
:numbers
%factor = %input.str.len
while ( %factors.count <= %input.str.len )
{
%factor = $calc(%factor * $calc(%input.str.len - %factors.count))
inc %factors.count
}
return %factor
}

This will work fine if you try doing it manually through the input box, but not as an alias or an identifier. Any ideas as to where I'm screwing up would be greatly appreciated.

Joined: Aug 2006
Posts: 183
T
Vogon poet
Offline
Vogon poet
T
Joined: Aug 2006
Posts: 183
You seem to have way too many variables. A much easier version is this:

Code:
Alias fac {
  var %i = $$1
  var %j = 1
  While %i > 1 { %j = $calc(%j * %i) | dec %i }
  return %j
}


It doesn't have error checking, but it works as expected when given proper input. Note that this version only will do anything useful if used as an identifier.

A slightly better version which checks if it was called as an identifier or not.

Code:
Alias fac {
  var %i = $$1
  var %j = 1
  While %i > 1 { %j = $calc(%j * %i) | dec %i }
  if ($isid) { return %j }
  else echo -a %j
}


And finally with a bit of error checking:

Code:
Alias fac {
  var %i = $$1
  if !$regex($$1,/^\d+$/) { echo -a Error: $$1 is not a number | halt }
  var %j = 1
  While %i > 1 { %j = $calc(%j * %i) | dec %i }
  if ($isid) { return %j }
  else echo -a %j
}

Last edited by Thrull; 29/12/09 07:37 PM.

Yar
Joined: Dec 2009
Posts: 3
D
Self-satisified door
OP Offline
Self-satisified door
D
Joined: Dec 2009
Posts: 3
thanks for the info, but I'm writing it the current way so it will also come up with every possible combination for a set of letters as well. that's the reason for the extra variables.

Joined: Aug 2006
Posts: 183
T
Vogon poet
Offline
Vogon poet
T
Joined: Aug 2006
Posts: 183
Well, if that's the case then I need more input on what you're trying to do. Its difficult to guess what the final output is supposed to be.

The only mistake I see is that

Code:
while ( %factors.count <= %input.str.len )
{


should be

Code:
while ( %factors.count <= %input.str.len ) {


Though, I'm not even positive that mirc won't misread that.

Last edited by Thrull; 29/12/09 07:44 PM.

Yar
Joined: Dec 2009
Posts: 3
D
Self-satisified door
OP Offline
Self-satisified door
D
Joined: Dec 2009
Posts: 3
Thanks so much for the reply. I studied your snip of code and finally realized my mistake...

The whole point of this project is to give me a # that I can base another loop off. I basically want to be able to feed it a string and get the # of all possible combinations by breaking the string down to the actual $len.

The final result will be a text file filled with different upper and lower case combinations:

for
For
FOr
FOR
foR
fOr
FoR
....

Thanks again for the help. I haven't done any scripting or programming in close to 5 years

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
It seems to me that you are simply using UPPER and lower case letters to represent binary.

Code:

alias combinations {
  if ($1 == $null) { echo -a /combinations <text> | return }
  var %t, %tt, %i = $$1
  var %n = 0, %nn = $base($str(1,$len(%i)),2,10)
  while (%n <= %nn) {
    %t = $null
    var %m = 0, %mm = $len(%i)
    while (%m < %mm) {
      inc %m
      %tt = $mid(%i,%m,1)
      %t = %t $+ $iif($mid($base(%n,10,2),%m,1) = 1,$upper(%tt),%tt)
    }
    inc %n
    echo -a %t
  }
  echo -a Found $calc(%nn + 1) combinations
}


- This will list all combinations of the first word only. It will require some modifications to support multiple words, due to how mIRC handles spaces. It will also accept non-letter characters without throwing an error, but it will attempt to make numbers and symbols into upper and lower case (which are the same character in mIRC). You can change the output from /echo to whatever form you need.

* trying to input a long word will lock up your mIRC for a long time, and likely disconnect you from any servers you are on due to ping timeouts.

-genius_at_work


Link Copied to Clipboard