Jump to content

Power


Recommended Posts

This is the code which I wrote with For loop to caculate Exponent..

It can be listed pow 1, pow2...,etc

Noraml Forloop ver.

$x = InputBox("Power", "Base:")
$y = InputBox("Power", "Exponent:")
MsgBox(0, "", powFor($x, $y))
Func powFor($x, $y)
    Dim $n, $t = $x, $c = 1, $p = 0
    If $y == 0 Then
        Return 1
    ElseIf $y < 0 Then
        For $i = StringRegExpReplace($y, '-(\d+)', '$1') To 1 Step - 1
            If $c = 1 Then
                $x *= 1
                $c += 1
                $p += 1
                $n &= "p" & $p & " : " & (1 / $x) & @CRLF
            Else
                $x *= $t
                $p += 1
                $n &= "p" & $p & " : " & (1 / $x) & @CRLF
            EndIf
        Next
    Else
        For $i = $y To 1 Step - 1
            If $c == 1 Then
                $x *= 1
                $c += 1
                $p += 1
                $n &= "p" & $p & " : " & $x & @CRLF
            Else
                $x *= $t
                $p += 1
                $n &= "p" & $p & " : " & $x & @CRLF
            EndIf
        Next
    EndIf
    Return $n
EndFunc

And this is the Recursion for caculating Exponent..

But how can I list each pow in Recursion ver. as For loop ver.

Recursion ver.

MsgBox(0, "", powRec($x, $y))
Func powRec($x, $y)
    If $y == 0 Then
        Return 1
    ElseIf $y == 1 Then
        Return $x
    Else
        Return $x * powRec($x, $y - 1)
    EndIf
EndFunc
Edited by Underdogger
Link to comment
Share on other sites

the simplest way - although probably will not be acceptable by your professor - is to dedicate a global var to accumulate the steps.

I made the following changes to your code:

declare global var $s

the func returns the computed value, not the string.

the func calculates it into $r before return, because $r is processed before return

if you want the func to return the string, without using any global var, then you will need to pass it as a 3rd parameter and perform some string manipulation.

PS how about some validation routines? your func fails with negative exponent!

Global $s=''
$x = InputBox("Power", "Base:")
$y = InputBox("Power", "Exponent:")
MsgBox(0, 'pow', powRecS($x, $y))
MsgBox(0, '$s', $s)

Func powRecS($x, $y)
Local $r
If $y == 0 Then
$r=1
ElseIf $y == 1 Then
$r=$x
Else
$r=$x * powRecS($x, $y - 1)
EndIf
$s=$s&' p'&$y&' : '&$r&@CR
Return $r
EndFunc

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

the simplest way - although probably will not be acceptable by your professor - is to dedicate a global var to accumulate the steps.

I made the following changes to your code:

declare global var $s

the func returns the computed value, not the string.

the func calculates it into $r before return, because $r is processed before return

if you want the func to return the string, without using any global var, then you will need to pass it as a 3rd parameter and perform some string manipulation.

PS how about some validation routines? your func fails with negative exponent!

Global $s=''
$x = InputBox("Power", "Base:")
$y = InputBox("Power", "Exponent:")
MsgBox(0, 'pow', powRecS($x, $y))
MsgBox(0, '$s', $s)

Func powRecS($x, $y)
Local $r
If $y == 0 Then
$r=1
ElseIf $y == 1 Then
$r=$x
Else
$r=$x * powRecS($x, $y - 1)
EndIf
$s=$s&' p'&$y&' : '&$r&@CR
Return $r
EndFunc

Is it possible to set negative Exponent in Recusion version?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...