How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Phix programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Phix programming language

Table of Contents

Problem Statement

Find the first perfect square in a given base N that has at least N digits and exactly N significant unique digits when expressed in base N. E.G. In base 10, the first perfect square with at least 10 unique digits is 1026753849 (32043²). You may use analytical methods to reduce the search space, but the code must do a search. Do not use magic numbers or just feed the code the answer to verify it is correct.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Phix programming language

Source code in the phix programming language

--          ?9/0 -- see below, inline part 1
            mask = length(sqi)
            #ilASM{
                    mov esi,[sqi]
                    mov edx,[mask]
                    shl esi,2
                    xor eax,eax
                  @@:
                    mov edi,1
                    mov cl,[esi]
                    shl edi,cl
                    add esi,4
                    or eax,edi
                    sub edx,1
                    jnz @b
                    mov [mask],eax
                  }
--and
--          ?9/0 --see below, inline part 2
            if length(dni)=length(sqi) then
                sqi = 0&sqi
            end if
            #ilASM{
                    mov esi,[sqi]
                    mov edi,[dni]
                    mov ecx,[ebx+esi*4-12]  -- length(sqi)
                    mov edx,[ebx+edi*4-12]  -- length(dni)
                    lea esi,[esi+ecx-1]
                    lea edi,[edi+edx-1]
                    sub ecx,edx
                    xor eax,eax
                    lea esi,[ebx+esi*4]     -- locate sqi[$]
                    lea edi,[ebx+edi*4]     -- locate dni[$]
                    push ecx
                    mov ecx,[base]
                  @@:
                    add eax,[esi]
                    add eax,[edi]
                    div cl
                    mov [esi],ah
                    xor ah,ah
                    sub esi,4
                    sub edi,4
                    sub edx,1
                    jnz @b
                    pop edx
                  @@:
                    test eax,eax
                    jz @f
                    add eax,[esi]
                    div cl
                    mov [esi],ah
                    xor ah,ah
                    sub esi,4
                    sub edx,1
                    jnz @b
                  @@:
                    mov [carry],eax
                  }


  

You may also check:How to resolve the algorithm Leap year step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the C# programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the Elixir programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the AWK programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the F# programming language