Task: Isqrt (integer square root) of X








VB: Imports System
Imports System.Console
Imports BI = System.Numerics.BigInteger

Module Module1
    Function isqrt(ByVal x As BI) As BI
        Dim t As BI, q As BI = 1, r As BI = 0
        While q <= x : q <<= 2 : End While
        While q > 1 : q >>= 2 : t = x - r - q : r >>= 1
            If t >= 0 Then x = t : r += q
        End While : Return r
    End Function

    Sub Main()
        Const max As Integer = 73, smax As Integer = 65
        Dim power_width As Integer = ((BI.Pow(7, max).ToString().Length \ 3) << 2) + 3,
            isqrt_width As Integer = (power_width + 1) >> 1,
            n as Integer
        WriteLine("Integer square root for numbers 0 to {0}:", smax)
        For n = 0 To smax : Write("{0} ", (n \ 10).ToString().Replace("0", " "))
            Next : WriteLine()
        For n = 0 To smax : Write("{0} ", n Mod 10) : Next : WriteLine()
        WriteLine(New String("-"c, (smax << 1) + 1))
        For n = 0 To smax : Write("{0} ", isqrt(n)) : Next
        WriteLine(vbLf & vbLf & "Integer square roots of odd powers of 7 from 1 to {0}:", max)
        Dim s As String = String.Format("[0,2] |[1,{0}:n0] |[2,{1}:n0]",
            power_width, isqrt_width).Replace("[", "{").Replace("]", "}")
        WriteLine(s, "n", "7 ^ n", "isqrt(7 ^ n)")
        WriteLine(New String("-"c, power_width + isqrt_width + 6))
        Dim p As BI = 7 : n = 1 : While n <= max
            WriteLine(s, n, p, isqrt(p)) : n += 2 : p = p * 49
        End While
    End Sub
End Module



Fortran: MODULE INTEGER_SQUARE_ROOT
    IMPLICIT NONE

    CONTAINS
        
    
    FUNCTION COMMATIZE(NUM) RESULT(OUT_STR)
         INTEGER(16), INTENT(IN) :: NUM
         INTEGER(16) I 
         CHARACTER(83) :: TEMP, OUT_STR
         
         WRITE(TEMP, '(I0)') NUM

         OUT_STR = ""

         DO I=0, LEN_TRIM(TEMP)-1
             IF (MOD(I, 3) .EQ. 0 .AND. I .GT. 0 .AND. I .LT. LEN_TRIM(TEMP)) THEN
                  OUT_STR = "," // TRIM(OUT_STR)
             END IF
             OUT_STR = TEMP(LEN_TRIM(TEMP)-I:LEN_TRIM(TEMP)-I) // TRIM(OUT_STR)
         END DO
    END FUNCTION COMMATIZE


    
    FUNCTION ISQRT(NUM)
        INTEGER(16), INTENT(IN) :: NUM
        INTEGER(16) :: ISQRT
        INTEGER(16) :: Q, Z, R, T
    
        Q = 1
        Z = NUM
        R = 0
        T = 0
    
        DO WHILE (Q .LT. NUM)
            Q = Q * 4
        END DO
    
        DO WHILE (Q .GT. 1)
            Q = Q / 4
            T = Z - R - Q
            R = R / 2
            
            IF (T .GE. 0) THEN
                Z = T
                R = R + Q
            END IF
        END DO
    
        ISQRT = R
    END FUNCTION ISQRT

END MODULE INTEGER_SQUARE_ROOT




PROGRAM ISQRT_DEMO
    USE INTEGER_SQUARE_ROOT
    IMPLICIT NONE


    INTEGER(16), PARAMETER :: MIN_NUM_HZ = 0
    INTEGER(16), PARAMETER :: MAX_NUM_HZ = 65
    INTEGER(16), PARAMETER :: POWER_BASE = 7
    INTEGER(16), PARAMETER :: POWER_MIN = 1
    INTEGER(16), PARAMETER :: POWER_MAX = 73
    INTEGER(16), DIMENSION(MAX_NUM_HZ-MIN_NUM_HZ+1) :: VALUES
    CHARACTER(2) :: HEADER_1
    CHARACTER(83) :: HEADER_2
    CHARACTER(83) :: HEADER_3

    INTEGER(16) :: I

    HEADER_1 = " n"
    HEADER_2 = "7^n"
    HEADER_3 = "isqrt(7^n)"

    WRITE(*,'(A, I0, A, I0)') "Integer square root for numbers ", MIN_NUM_HZ, " to ", MAX_NUM_HZ

    DO I=1, SIZE(VALUES)
        VALUES(I) = ISQRT(MIN_NUM_HZ+I)
    END DO

    WRITE(*,'(100I2)') VALUES
    WRITE(*,*) NEW_LINE('A')
    
    WRITE(*,'(A,A,A,A,A)') HEADER_1, " | ", HEADER_2, " | ", HEADER_3
    WRITE(*,*) REPEAT("-", 8+83*2)

    DO I=POWER_MIN,POWER_MAX, 2
        WRITE(*,'(I2, A, A, A, A)') I, " | " // COMMATIZE(7**I), " | ", COMMATIZE(ISQRT(7**I))
    END DO
        

 END PROGRAM ISQRT_DEMO