1234 ** 1234 ; is it correct, can it bettered?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

1234 ** 1234 ; is it correct, can it bettered?

18 Jul 2015, 12:39

Code: Select all

SetBatchLines -1
input_number := 1234
power := 1234

constant_to_be_multiplied := input_number
array1 := strsplit(input_number)
TO_Carry := 0
carry_carry := 0

loop % array1.maxindex()+1
    end_result .= "0"

array_Total := strsplit(end_result)

;===========================================
loop, % power - 1
{
    ;~ MsgBox % end_result
if ( end_result = 0 )
    end_result := input_number



end_result := "0" . end_Result
array := strsplit(end_Result)

;~ MsgBox % array.maxindex()

;===multiplying========
;~ MsgBox % End_result . "*" . input_number ;================================================= IMPORTANT===========================================
loop, % array1.maxindex()
{
    zeroes := A_index
    digit_to_multiply_with_number := array1[array1.maxindex()- A_index + 1]
    loop, % array.maxindex()
    {
    result := digit_to_multiply_with_number * array[array.maxindex() - A_index + 1] + To_Carry
    TO_CARRY := result//10
    last_digit := mod(result, 10)
    required_line := last_digit . required_line
    }
    
    TO_CARRY := 0
    
    loop, % zeroes - 1
        zeroes_ .= "0"
    
    ;~ MsgBox % required_line . zeroes_
    lines_by_lines := strsplit(required_line . zeroes_)
    
    ;~ MsgBox % lines_by_lines[1]
    
    ;=========final line processing =====ADDITION====================
    ;~ MsgBox % "new total is:" . "`n" . end_result
    
    if ( A_index = 1 )
    {
        ;~ MsgBox % is_this_the_Result . "`n" . A_index . "`n" . checking_last_Step
        array_Total := strsplit(end_Result)
        
        loop % array_Total.maxindex()+1
        end_result .= "0"
        
        array_Total := strsplit(end_result)
    }
    loop, % array_Total.maxindex() > lines_by_lines.maxindex() ? array_Total.maxindex() : lines_by_lines.maxindex()
    {
        ;~ MsgBox % array_Total.maxindex() > lines_by_lines.maxindex() ? array_Total.maxindex() : lines_by_lines.maxindex()
        if ( array_Total[array_Total.maxindex()-A_index+1] = "" )
            array_Total[array_Total.maxindex()-A_index+1] := 0
        ;~ MsgBox % array_Total[array_Total.maxindex()-A_index+1]
        ;~ MsgBox % array_Total[array_Total.maxindex()-A_index+1] . "wwwww`n" .  lines_by_lines[lines_by_lines.maxindex()-A_index+1] . "qqqqq`n" .  carry_carry . "eee"
        ADD_last_digits := lines_by_lines[lines_by_lines.maxindex()-A_index+1] + array_Total[array_Total.maxindex()-A_index+1] + carry_carry
        last_last_digit := mod(ADD_last_digits,10)
        carry_Carry :=ADD_last_digits//10 
        is_this_the_Result := last_last_digit . is_this_the_Result
    }
        
        array_Total := strsplit(is_this_the_Result)
        checking_last_Step := is_this_the_Result
        ;~ MsgBox % is_this_the_Result . "`n" . A_index . "`n" . checking_last_Step
        is_this_the_Result := ""
        carry_carry := 0
    ;===============================================
    
    zeroes := ""
    required_line := ""
    zeroes_ := ""
    
    
}
;==================

end_Result := RegExReplace(checking_last_Step, "(0)?(\d+)", "$2") ;==================================================================================IMPORTANT
;~ end_result := checking_last_step

if ( power -1 = A_index )
{
    ;~ checking_alst_Step := 
MsgBox % input_number . "**" . power . ":`n" . checking_last_step
ExitApp
}
;~ end_Result := RegExReplace(end_Result, "\d", "0")
array_Total := strsplit(end_Result)

}
;~ MsgBox % end_Result := "0" . checking_last_Step
ExitApp
esc::ExitApp
John ... you working ?
just me
Posts: 9532
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: 1234 ** 1234 ; is it correct, can it bettered?

19 Jul 2015, 10:52

According to the Big Integer Calculator it is correct.

This is the fastest I could get, though still rather slow. A MCode function for the multiplication would perform much better.

Code: Select all

#NoEnv
SetBatchLines -1
input_number := 1234
power := 1234
T := A_TickCount
Result := Power(input_number, power)
T := A_TickCount - T
N := StrLen(Result)
MsgBox, 0, %N% digits in %T% ms, %input_number%**%Power%=`n`n%Result%
ExitApp
Power(Base, Exponent) {
   ; Base and Exponent must be unsigned integer values.
   ; B : reversed Base buffer
   ; R : reversed Result buffer
   ; P : previous Result
   SLB := StrLen(Base)
   VarSetCapacity(B, SLB, 0)
   Loop, Parse, Base
      NumPut(A_LoopField, B, SLB - A_Index, "UChar")
   VarSetCapacity(P, SLB, 0)
   DllCall("RtlMoveMemory", "Ptr", &P, "Ptr", &B, "Ptr", PL := SLB)
   Loop, % (Exponent - 1) {
      V := 0, VarSetCapacity(R, RL := PL + SLB + 1, 0)
      Loop, %SLB% { ; multiply
         If (I := NumGet(B, A_Index - 1, "UChar")) {
            Loop, % PL
               N := NumGet(R, J := A_Index + V - 1, "UChar") + (NumGet(P, A_Index - 1, "UChar") * I)
               , NumPut(SubStr(N, 0), R, J++, "UChar")
               , NumPut(NumGet(R, J, "UChar") + SubStr("0" . N, -1, 1), R, J, "UChar")
         }
         V++
      }
      VarSetCapacity(P, RL, 0), DllCall("RtlMoveMemory", "Ptr", &P, "Ptr", &R, "Ptr", PL := RL)
      While !NumGet(P, PL - 1, "UChar") ; remove trailing zeroes
         PL--
   }
   VarSetCapacity(V, RL, 0)
   Loop, % RL
      V .= NumGet(R, --RL, "UChar")
   Return LTrim(V, "0")
}
Interestingly, using binary buffers and NumGet/Numput() is significant faster than real arrays, at least for the original task (1234**1234).
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: 1234 ** 1234 ; is it correct, can it bettered?

24 Jul 2015, 19:35

@just me
thanks :)

will help me a lot. :)
John ... you working ?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], CodeKiller, just me and 179 guests