WeixiangYan/CodeTransOcean huggingface dataset:
Task: Square but not cube







Python: 
def nonCubeSquares(n):
    upto = enumFromTo(1)
    ns = upto(n)
    setCubes = set(x ** 3 for x in ns)
    ms = upto(n + len(set(x * x for x in ns).intersection(
        setCubes
    )))
    return list(tuple([x * x, x in setCubes]) for x in ms)



def squareListing(xs):
    justifyIdx = justifyRight(len(str(1 + len(xs))))(' ')
    justifySqr = justifyRight(1 + len(str(xs[-1][0])))(' ')
    return list(
        '(' + str(1 + idx) + '^2 = ' + str(n) +
        ' = ' + str(round(n ** (1 / 3))) + '^3)' if bln else (
            justifyIdx(1 + idx) + ' ->' +
            justifySqr(n)
        )
        for idx, (n, bln) in enumerate(xs)
    )


def main():
    print(
        unlines(
            squareListing(
                nonCubeSquares(30)
            )
        )
    )





def enumFromTo(m):
    return lambda n: list(range(m, 1 + n))



def justifyRight(n):
    return lambda cFiller: lambda a: (
        ((n * cFiller) + str(a))[-n:]
    )



def unlines(xs):
    return '\n'.join(xs)


main()



COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. SQUARE-NOT-CUBE.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 COMPUTATION.
           02 SQ-ROOT       PIC 9999 COMP VALUE 1.
           02 CUBE-ROOT     PIC 9999 COMP VALUE 1.
           02 SQUARE        PIC 9999 COMP VALUE 1.
           02 CUBE          PIC 9999 COMP VALUE 1.
           02 SEEN          PIC 99   COMP VALUE 0.
        01 OUTPUT-FORMAT.
           02 OUT-NUM       PIC ZZZ9.
        
        PROCEDURE DIVISION.
        SQUARE-STEP.
            COMPUTE SQUARE = SQ-ROOT ** 2.
        CUBE-STEP.
            IF SQUARE IS GREATER THAN CUBE
                ADD 1 TO CUBE-ROOT
                COMPUTE CUBE = CUBE-ROOT ** 3
                GO TO CUBE-STEP.
            IF SQUARE IS NOT EQUAL TO CUBE
                ADD 1 TO SEEN
                MOVE SQUARE TO OUT-NUM
                DISPLAY OUT-NUM.
            ADD 1 TO SQ-ROOT.
            IF SEEN IS LESS THAN 30 GO TO SQUARE-STEP.
            STOP RUN.
Task: Square but not cube








VB: Module Module1

  
  
  
  

  
  Function ChkFlg(flag As Integer, ByRef mask As Integer) As Boolean
    ChkFlg = (flag And mask) = mask : mask <<= 1
  End Function

  Sub SwoC(limit As Integer)
    Dim count, square, delta, cube, d1, d2, flag, mask As Integer, s as string = ""
    count = 1 : square = 1 : delta = 1 : cube = 1 : d1 = 1 : d2 = 0
    While count <= limit
      flag = {5, 7, 2}(1 + square.CompareTo(cube))
      If flag = 7 Then s = String. Format("   {0} (also cube)", square)
      If flag = 5 Then s = String.Format("{0,-2} {1}", count, square) : count += 1
      mask = 1 : If ChkFlg(flag, mask) Then delta += 2 : square += delta
      If ChkFlg(flag, mask) Then d2 += 6 : d1 += d2 : cube += d1
      If ChkFlg(flag, mask) Then Console.WriteLine(s)
    End While
  End Sub

  Sub Main()
    SwoC(30)
  End Sub

End Module


COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. SQUARE-NOT-CUBE.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 COMPUTATION.
           02 SQ-ROOT       PIC 9999 COMP VALUE 1.
           02 CUBE-ROOT     PIC 9999 COMP VALUE 1.
           02 SQUARE        PIC 9999 COMP VALUE 1.
           02 CUBE          PIC 9999 COMP VALUE 1.
           02 SEEN          PIC 99   COMP VALUE 0.
        01 OUTPUT-FORMAT.
           02 OUT-NUM       PIC ZZZ9.
        
        PROCEDURE DIVISION.
        SQUARE-STEP.
            COMPUTE SQUARE = SQ-ROOT ** 2.
        CUBE-STEP.
            IF SQUARE IS GREATER THAN CUBE
                ADD 1 TO CUBE-ROOT
                COMPUTE CUBE = CUBE-ROOT ** 3
                GO TO CUBE-STEP.
            IF SQUARE IS NOT EQUAL TO CUBE
                ADD 1 TO SEEN
                MOVE SQUARE TO OUT-NUM
                DISPLAY OUT-NUM.
            ADD 1 TO SQ-ROOT.
            IF SEEN IS LESS THAN 30 GO TO SQUARE-STEP.
            STOP RUN.
Task: Square but not cube




Go: package main

import (
    "fmt"
    "math"
)

func main() {
    for n, count := 1, 0; count < 30; n++ {
        sq := n * n
        cr := int(math.Cbrt(float64(sq)))
        if cr*cr*cr != sq {
            count++
            fmt.Println(sq)
        } else {
            fmt.Println(sq, "is square and cube")
        }
    }
}






COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. SQUARE-NOT-CUBE.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 COMPUTATION.
           02 SQ-ROOT       PIC 9999 COMP VALUE 1.
           02 CUBE-ROOT     PIC 9999 COMP VALUE 1.
           02 SQUARE        PIC 9999 COMP VALUE 1.
           02 CUBE          PIC 9999 COMP VALUE 1.
           02 SEEN          PIC 99   COMP VALUE 0.
        01 OUTPUT-FORMAT.
           02 OUT-NUM       PIC ZZZ9.
        
        PROCEDURE DIVISION.
        SQUARE-STEP.
            COMPUTE SQUARE = SQ-ROOT ** 2.
        CUBE-STEP.
            IF SQUARE IS GREATER THAN CUBE
                ADD 1 TO CUBE-ROOT
                COMPUTE CUBE = CUBE-ROOT ** 3
                GO TO CUBE-STEP.
            IF SQUARE IS NOT EQUAL TO CUBE
                ADD 1 TO SEEN
                MOVE SQUARE TO OUT-NUM
                DISPLAY OUT-NUM.
            ADD 1 TO SQ-ROOT.
            IF SEEN IS LESS THAN 30 GO TO SQUARE-STEP.
            STOP RUN.
Task: Egyptian division

C: '---Ported from the c code example to BaCon by bigbass 

'==================================================================================
FUNCTION EGYPTIAN_DIVISION(long dividend, long divisor, long remainder) TYPE long 
'==================================================================================
'--- remainder is the third  parameter, pass 0 if you do not need the remainder
 
DECLARE powers[64] TYPE long 
DECLARE doublings[64] TYPE long 

	LOCAL i TYPE long
 
	FOR i = 0 TO  63 STEP 1  
		powers[i] = 1 << i
		doublings[i] = divisor << i
		IF (doublings[i] > dividend) THEN
			BREAK
		ENDIF
	NEXT

	LOCAL answer TYPE long
	LOCAL accumulator TYPE long
	answer = 0
	accumulator = 0
 
	WHILE i >= 0
		'--- If the current value of the accumulator added to the
		'--- doublings cell would be less than or equal to the
		'--- dividend then add it to the accumulator
		IF (accumulator + doublings[i] <= dividend) THEN
			accumulator = accumulator + doublings[i]
			answer = answer + powers[i]
		ENDIF
		DECR i
	WEND
 
	IF remainder THEN
		remainder = dividend - accumulator
		PRINT dividend ," / ", divisor, " = " , answer ," remainder " , remainder

        PRINT "Decoded the answer to a standard fraction"
        PRINT  (remainder + 0.0 )/ (divisor + 0.0) + answer
        PRINT

	ELSE 
		PRINT dividend ," / ", divisor , " = " , answer 
	ENDIF
	
	RETURN answer

ENDFUNCTION

 
	'--- the large number divided by the smaller number 
	'--- the third argument is 1 if you want to have a remainder
	'--- and 0 if you dont want to have a remainder 
	
	EGYPTIAN_DIVISION(580,34,1)
	EGYPTIAN_DIVISION(580,34,0)

EGYPTIAN_DIVISION(580,34,1)











Ada: with Ada.Text_IO;

procedure Egyptian_Division is

  procedure Divide  (a : Natural; b : Positive; q, r : out Natural) is
    doublings : array (0..31) of Natural;  
    m, sum, last_index_touched : Natural := 0;    
  begin
    for i in doublings'Range loop
      m := b * 2**i; 
      exit when m > a ;
      doublings (i) := m;
      last_index_touched := i;
    end loop;
    q := 0;
    for i in reverse doublings'First .. last_index_touched loop
        m := sum + doublings (i);
        if m <= a then 
          sum := m; 
          q := q + 2**i;
        end if;
    end loop;
    r := a -sum;
  end Divide;
  
  q, r : Natural;
begin
  Divide (580,34, q, r);
  Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
Task: Egyptian division


C++: #include <cassert>
#include <iostream>

typedef unsigned long ulong;


ulong egyptian_division(ulong dividend, ulong divisor, ulong* remainder) {
    constexpr int SIZE = 64;
    ulong powers[SIZE];
    ulong doublings[SIZE];
    int i = 0;

    for (; i < SIZE; ++i) {
        powers[i] = 1 << i;
        doublings[i] = divisor << i;
        if (doublings[i] > dividend) {
            break;
        }
    }

    ulong answer = 0;
    ulong accumulator = 0;

    for (i = i - 1; i >= 0; --i) {
        
        if (accumulator + doublings[i] <= dividend) {
            accumulator += doublings[i];
            answer += powers[i];
        }
    }

    if (remainder) {
        *remainder = dividend - accumulator;
    }
    return answer;
}

void print(ulong a, ulong b) {
    using namespace std;

    ulong x, y;
    x = egyptian_division(a, b, &y);

    cout << a << " / " << b << " = " << x << " remainder " << y << endl;
    assert(a == b * x + y);
}

int main() {
    print(580, 34);

    return 0;
}










Ada: with Ada.Text_IO;

procedure Egyptian_Division is

  procedure Divide  (a : Natural; b : Positive; q, r : out Natural) is
    doublings : array (0..31) of Natural;  
    m, sum, last_index_touched : Natural := 0;    
  begin
    for i in doublings'Range loop
      m := b * 2**i; 
      exit when m > a ;
      doublings (i) := m;
      last_index_touched := i;
    end loop;
    q := 0;
    for i in reverse doublings'First .. last_index_touched loop
        m := sum + doublings (i);
        if m <= a then 
          sum := m; 
          q := q + 2**i;
        end if;
    end loop;
    r := a -sum;
  end Divide;
  
  q, r : Natural;
begin
  Divide (580,34, q, r);
  Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
Task: Egyptian division




Go: package main

import "fmt"

func egyptianDivide(dividend, divisor int) (quotient, remainder int) {
    if dividend < 0 || divisor <= 0 {
        panic("Invalid argument(s)")
    }
    if dividend < divisor {
        return 0, dividend
    }
    powersOfTwo := []int{1}
    doublings := []int{divisor}
    doubling := divisor
    for {
        doubling *= 2
        if doubling > dividend {
            break
        }
        l := len(powersOfTwo)
        powersOfTwo = append(powersOfTwo, powersOfTwo[l-1]*2)
        doublings = append(doublings, doubling)
    }
    answer := 0
    accumulator := 0
    for i := len(doublings) - 1; i >= 0; i-- {
        if accumulator+doublings[i] <= dividend {
            accumulator += doublings[i]
            answer += powersOfTwo[i]
            if accumulator == dividend {
                break
            }
        }
    }
    return answer, dividend - accumulator
}

func main() {
    dividend := 580
    divisor := 34
    quotient, remainder := egyptianDivide(dividend, divisor)
    fmt.Println(dividend, "divided by", divisor, "is", quotient, "with remainder", remainder)
}








Ada: with Ada.Text_IO;

procedure Egyptian_Division is

  procedure Divide  (a : Natural; b : Positive; q, r : out Natural) is
    doublings : array (0..31) of Natural;  
    m, sum, last_index_touched : Natural := 0;    
  begin
    for i in doublings'Range loop
      m := b * 2**i; 
      exit when m > a ;
      doublings (i) := m;
      last_index_touched := i;
    end loop;
    q := 0;
    for i in reverse doublings'First .. last_index_touched loop
        m := sum + doublings (i);
        if m <= a then 
          sum := m; 
          q := q + 2**i;
        end if;
    end loop;
    r := a -sum;
  end Divide;
  
  q, r : Natural;
begin
  Divide (580,34, q, r);
  Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
Task: Egyptian division





Java: import java.util.ArrayList;
import java.util.List;

public class EgyptianDivision {

    
    public static void main(String[] args) {

        divide(580, 34);

    }

    
    public static void divide(int dividend, int divisor) {

        List<Integer> powersOf2 = new ArrayList<>();
        List<Integer> doublings = new ArrayList<>();

        
        int line = 0;
        while ((Math.pow(2, line) * divisor) <= dividend) { 
            int powerOf2 = (int) Math.pow(2, line);
            powersOf2.add(powerOf2);
            doublings.add(powerOf2 * divisor);
            line++;
        }

        int answer = 0;
        int accumulator = 0;

        
        for (int i = powersOf2.size() - 1; i >= 0; i--) {
            if (accumulator + doublings.get(i) <= dividend) {
                accumulator += doublings.get(i);
                answer += powersOf2.get(i);
            }
        }

        System.out.println(String.format("%d, remainder %d", answer, dividend - accumulator));
    }
}







Ada: with Ada.Text_IO;

procedure Egyptian_Division is

  procedure Divide  (a : Natural; b : Positive; q, r : out Natural) is
    doublings : array (0..31) of Natural;  
    m, sum, last_index_touched : Natural := 0;    
  begin
    for i in doublings'Range loop
      m := b * 2**i; 
      exit when m > a ;
      doublings (i) := m;
      last_index_touched := i;
    end loop;
    q := 0;
    for i in reverse doublings'First .. last_index_touched loop
        m := sum + doublings (i);
        if m <= a then 
          sum := m; 
          q := q + 2**i;
        end if;
    end loop;
    r := a -sum;
  end Divide;
  
  q, r : Natural;
begin
  Divide (580,34, q, r);
  Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
Task: Egyptian division







Python: from itertools import product

def egyptian_divmod(dividend, divisor):
    assert divisor != 0
    pwrs, dbls = [1], [divisor]
    while dbls[-1] <= dividend:
        pwrs.append(pwrs[-1] * 2)
        dbls.append(pwrs[-1] * divisor)
    ans, accum = 0, 0
    for pwr, dbl in zip(pwrs[-2::-1], dbls[-2::-1]):
        if accum + dbl <= dividend:
            accum += dbl
            ans += pwr
    return ans, abs(accum - dividend)

if __name__ == "__main__":
    
    for i, j in product(range(13), range(1, 13)):
            assert egyptian_divmod(i, j) == divmod(i, j)
    
    i, j = 580, 34
    print(f'{i} divided by {j} using the Egyption method is %i remainder %i'
          % egyptian_divmod(i, j))





Ada: with Ada.Text_IO;

procedure Egyptian_Division is

  procedure Divide  (a : Natural; b : Positive; q, r : out Natural) is
    doublings : array (0..31) of Natural;  
    m, sum, last_index_touched : Natural := 0;    
  begin
    for i in doublings'Range loop
      m := b * 2**i; 
      exit when m > a ;
      doublings (i) := m;
      last_index_touched := i;
    end loop;
    q := 0;
    for i in reverse doublings'First .. last_index_touched loop
        m := sum + doublings (i);
        if m <= a then 
          sum := m; 
          q := q + 2**i;
        end if;
    end loop;
    r := a -sum;
  end Divide;
  
  q, r : Natural;
begin
  Divide (580,34, q, r);
  Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
Task: Egyptian division








VB: Option Explicit

Private Type MyTable
    powers_of_2 As Long
    doublings As Long
End Type

Private Type Assemble
    answer As Long
    accumulator As Long
End Type

Private Type Division
    Quotient As Long
    Remainder As Long
End Type

Private Type DivEgyp
    Dividend As Long
    Divisor As Long
End Type

Private Deg As DivEgyp

Sub Main()
Dim d As Division
    Deg.Dividend = 580
    Deg.Divisor = 34
    d = Divise(CreateTable)
    Debug.Print "Quotient = " & d.Quotient & " Remainder = " & d.Remainder
End Sub

Private Function CreateTable() As MyTable()
Dim t() As MyTable, i As Long
    Do
        i = i + 1
        ReDim Preserve t(i)
        t(i).powers_of_2 = 2 ^ (i - 1)
        t(i).doublings = Deg.Divisor * t(i).powers_of_2
    Loop While 2 * t(i).doublings <= Deg.Dividend
    CreateTable = t
End Function

Private Function Divise(t() As MyTable) As Division
Dim a As Assemble, i As Long
    a.accumulator = 0
    a.answer = 0
    For i = UBound(t) To LBound(t) Step -1
        If a.accumulator + t(i).doublings <= Deg.Dividend Then
            a.accumulator = a.accumulator + t(i).doublings
            a.answer = a.answer + t(i).powers_of_2
        End If
    Next
    Divise.Quotient = a.answer
    Divise.Remainder = Deg.Dividend - a.accumulator
End Function




Ada: with Ada.Text_IO;

procedure Egyptian_Division is

  procedure Divide  (a : Natural; b : Positive; q, r : out Natural) is
    doublings : array (0..31) of Natural;  
    m, sum, last_index_touched : Natural := 0;    
  begin
    for i in doublings'Range loop
      m := b * 2**i; 
      exit when m > a ;
      doublings (i) := m;
      last_index_touched := i;
    end loop;
    q := 0;
    for i in reverse doublings'First .. last_index_touched loop
        m := sum + doublings (i);
        if m <= a then 
          sum := m; 
          q := q + 2**i;
        end if;
    end loop;
    r := a -sum;
  end Divide;
  
  q, r : Natural;
begin
  Divide (580,34, q, r);
  Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
Task: Egyptian division

C: '---Ported from the c code example to BaCon by bigbass 

'==================================================================================
FUNCTION EGYPTIAN_DIVISION(long dividend, long divisor, long remainder) TYPE long 
'==================================================================================
'--- remainder is the third  parameter, pass 0 if you do not need the remainder
 
DECLARE powers[64] TYPE long 
DECLARE doublings[64] TYPE long 

	LOCAL i TYPE long
 
	FOR i = 0 TO  63 STEP 1  
		powers[i] = 1 << i
		doublings[i] = divisor << i
		IF (doublings[i] > dividend) THEN
			BREAK
		ENDIF
	NEXT

	LOCAL answer TYPE long
	LOCAL accumulator TYPE long
	answer = 0
	accumulator = 0
 
	WHILE i >= 0
		'--- If the current value of the accumulator added to the
		'--- doublings cell would be less than or equal to the
		'--- dividend then add it to the accumulator
		IF (accumulator + doublings[i] <= dividend) THEN
			accumulator = accumulator + doublings[i]
			answer = answer + powers[i]
		ENDIF
		DECR i
	WEND
 
	IF remainder THEN
		remainder = dividend - accumulator
		PRINT dividend ," / ", divisor, " = " , answer ," remainder " , remainder

        PRINT "Decoded the answer to a standard fraction"
        PRINT  (remainder + 0.0 )/ (divisor + 0.0) + answer
        PRINT

	ELSE 
		PRINT dividend ," / ", divisor , " = " , answer 
	ENDIF
	
	RETURN answer

ENDFUNCTION

 
	'--- the large number divided by the smaller number 
	'--- the third argument is 1 if you want to have a remainder
	'--- and 0 if you dont want to have a remainder 
	
	EGYPTIAN_DIVISION(580,34,1)
	EGYPTIAN_DIVISION(580,34,0)

EGYPTIAN_DIVISION(580,34,1)
Task: Egyptian division


C++: #include <cassert>
#include <iostream>

typedef unsigned long ulong;


ulong egyptian_division(ulong dividend, ulong divisor, ulong* remainder) {
    constexpr int SIZE = 64;
    ulong powers[SIZE];
    ulong doublings[SIZE];
    int i = 0;

    for (; i < SIZE; ++i) {
        powers[i] = 1 << i;
        doublings[i] = divisor << i;
        if (doublings[i] > dividend) {
            break;
        }
    }

    ulong answer = 0;
    ulong accumulator = 0;

    for (i = i - 1; i >= 0; --i) {
        
        if (accumulator + doublings[i] <= dividend) {
            accumulator += doublings[i];
            answer += powers[i];
        }
    }

    if (remainder) {
        *remainder = dividend - accumulator;
    }
    return answer;
}

void print(ulong a, ulong b) {
    using namespace std;

    ulong x, y;
    x = egyptian_division(a, b, &y);

    cout << a << " / " << b << " = " << x << " remainder " << y << endl;
    assert(a == b * x + y);
}

int main() {
    print(580, 34);

    return 0;
}
Task: Egyptian division





Java: import java.util.ArrayList;
import java.util.List;

public class EgyptianDivision {

    
    public static void main(String[] args) {

        divide(580, 34);

    }

    
    public static void divide(int dividend, int divisor) {

        List<Integer> powersOf2 = new ArrayList<>();
        List<Integer> doublings = new ArrayList<>();

        
        int line = 0;
        while ((Math.pow(2, line) * divisor) <= dividend) { 
            int powerOf2 = (int) Math.pow(2, line);
            powersOf2.add(powerOf2);
            doublings.add(powerOf2 * divisor);
            line++;
        }

        int answer = 0;
        int accumulator = 0;

        
        for (int i = powersOf2.size() - 1; i >= 0; i--) {
            if (accumulator + doublings.get(i) <= dividend) {
                accumulator += doublings.get(i);
                answer += powersOf2.get(i);
            }
        }

        System.out.println(String.format("%d, remainder %d", answer, dividend - accumulator));
    }
}
Task: Egyptian division







Python: from itertools import product

def egyptian_divmod(dividend, divisor):
    assert divisor != 0
    pwrs, dbls = [1], [divisor]
    while dbls[-1] <= dividend:
        pwrs.append(pwrs[-1] * 2)
        dbls.append(pwrs[-1] * divisor)
    ans, accum = 0, 0
    for pwr, dbl in zip(pwrs[-2::-1], dbls[-2::-1]):
        if accum + dbl <= dividend:
            accum += dbl
            ans += pwr
    return ans, abs(accum - dividend)

if __name__ == "__main__":
    
    for i, j in product(range(13), range(1, 13)):
            assert egyptian_divmod(i, j) == divmod(i, j)
    
    i, j = 580, 34
    print(f'{i} divided by {j} using the Egyption method is %i remainder %i'
          % egyptian_divmod(i, j))
Task: Egyptian division








VB: Option Explicit

Private Type MyTable
    powers_of_2 As Long
    doublings As Long
End Type

Private Type Assemble
    answer As Long
    accumulator As Long
End Type

Private Type Division
    Quotient As Long
    Remainder As Long
End Type

Private Type DivEgyp
    Dividend As Long
    Divisor As Long
End Type

Private Deg As DivEgyp

Sub Main()
Dim d As Division
    Deg.Dividend = 580
    Deg.Divisor = 34
    d = Divise(CreateTable)
    Debug.Print "Quotient = " & d.Quotient & " Remainder = " & d.Remainder
End Sub

Private Function CreateTable() As MyTable()
Dim t() As MyTable, i As Long
    Do
        i = i + 1
        ReDim Preserve t(i)
        t(i).powers_of_2 = 2 ^ (i - 1)
        t(i).doublings = Deg.Divisor * t(i).powers_of_2
    Loop While 2 * t(i).doublings <= Deg.Dividend
    CreateTable = t
End Function

Private Function Divise(t() As MyTable) As Division
Dim a As Assemble, i As Long
    a.accumulator = 0
    a.answer = 0
    For i = UBound(t) To LBound(t) Step -1
        If a.accumulator + t(i).doublings <= Deg.Dividend Then
            a.accumulator = a.accumulator + t(i).doublings
            a.answer = a.answer + t(i).powers_of_2
        End If
    Next
    Divise.Quotient = a.answer
    Divise.Remainder = Deg.Dividend - a.accumulator
End Function
Task: Egyptian division




Go: package main

import "fmt"

func egyptianDivide(dividend, divisor int) (quotient, remainder int) {
    if dividend < 0 || divisor <= 0 {
        panic("Invalid argument(s)")
    }
    if dividend < divisor {
        return 0, dividend
    }
    powersOfTwo := []int{1}
    doublings := []int{divisor}
    doubling := divisor
    for {
        doubling *= 2
        if doubling > dividend {
            break
        }
        l := len(powersOfTwo)
        powersOfTwo = append(powersOfTwo, powersOfTwo[l-1]*2)
        doublings = append(doublings, doubling)
    }
    answer := 0
    accumulator := 0
    for i := len(doublings) - 1; i >= 0; i-- {
        if accumulator+doublings[i] <= dividend {
            accumulator += doublings[i]
            answer += powersOfTwo[i]
            if accumulator == dividend {
                break
            }
        }
    }
    return answer, dividend - accumulator
}

func main() {
    dividend := 580
    divisor := 34
    quotient, remainder := egyptianDivide(dividend, divisor)
    fmt.Println(dividend, "divided by", divisor, "is", quotient, "with remainder", remainder)
}