Android Code Snippet Translation from C to B4A

Someone experienced in C language can help me translate this code in B4A?
I tried but my code does not work.
Thank you


// Factorize 'value' and save result in 'res'

void factorize(char*res, int value)
{
res[0] ='\0';
if (is_prime(value))
{
sprintf(res, "%d", value);
return;
}
for (int i =2; value >1&& i <= value; ++i)
{
if (!is_prime(i)) continue;
int exp;
for (exp =0; value%i==0; value/=i, ++exp);
if (!exp) continue;
if (exp >1) sprintf(res, "%s%d^%d", res, i, exp);
else sprintf(res, "%s%d", res, i);
if (value >1) sprintf(res, "%s * ", res);
}
return;
}


bool is_prime(int value)
{
for (int i =2; i <= sqrt(value); ++i)
if (value%i==0) returnfalse;
returntrue;
}
 

giacomo-italy

Member
Licensed User
Longtime User
This is my code, but does not work:

B4X:
Sub factorize(value As Int) As String
Dim res As String
Dim exp As Int
'{
    'res[0] = '\0';
    If (is_prime(value)=1) Then
'    {
        res= value
        Return res
'    }
     End If
   
    Dim i As Int
    'For (int i = 2; value > 1 && i <= value; ++i)
     i = 2
     Do While (value > 1 And i <= value)
'   {
        If (is_prime(i)=0) Then Continue
        'Dim exp As Int
       
        'For (exp = 0; value%i==0; value=value/i, ++exp);
        exp = 0
        Do While ((value mod i)=0)
         If (exp=0) Then Continue
         value=value/i
         exp = exp +1
        Loop
       
        If (exp > 1) Then
         res=res & i & "^" & exp 'sprintf(res, "%s%d^%d", res, i, exp);
        Else
         res=res & i'sprintf(res, "%s%d", res, i);
        End If
       
         If (value > 1) Then res=res & " * "'sprintf(res, "%s * ", res);
'    }
        i=i+1
    Loop
   
    Return res
'}

End Sub

Sub is_prime(value As Int)As Int
   
    Dim i As Int
    For i = 2 To Sqrt(value)
        If ((value mod i) =0) Then Return 0
    Next
   
Return 1

End Sub
 

giacomo-italy

Member
Licensed User
Longtime User
Code in C that i need to traslate:

B4X:
// Factorize 'value' and save result in 'res'
void factorize(char*res, int value)
{
res[0] ='\0';
if (is_prime(value))
{
sprintf(res, "%d", value);
return;
}
for (int i =2; value >1&& i <= value; ++i)
{
if (!is_prime(i)) continue;
int exp;
for (exp =0; value%i==0; value/=i, ++exp);
if (!exp) continue;
if (exp >1) sprintf(res, "%s%d^%d", res, i, exp);
else sprintf(res, "%s%d", res, i);
if (value >1) sprintf(res, "%s * ", res);
}
return;
}


bool is_prime(int value)
{
for (int i =2; i <= sqrt(value); ++i)
if (value%i==0) returnfalse;
returntrue;
}
 

Myr0n

Active Member
Licensed User
Longtime User
Should be something like this

B4X:
Sub factorize(value As Int) As String
Dim res As String = ""

If (is_prime(value)) Then
    res = value
    Return res
End If
Dim i As Int = 2
Do While (value >1 And i<=value)
    i=i+1   
    If Not(is_prime(i)) Then
        Continue
    End If
    Dim exp As Int
    Do While (value Mod i)=0
        value = value/i
        If (exp > 1) Then
            res=res & i & "^" & exp 'sprintf(res, "%s%d^%d", res, i, exp);           
        Else
            res=res & i'sprintf(res, "%s%d", res, i);
        End If      
        If (value > 1) Then res=res & " * " 'sprintf(res, "%s * ", res);       
        exp=exp+1           
    Loop
Loop
Return res
End Sub

Sub is_prime(value As Int) As Boolean   
    Dim i As Int = 2
    For i=2 To Sqrt(value) Step 1
        If (value Mod i)=0 Then
            Return False
        End If
    Next
    Return True
End Sub
 

giacomo-italy

Member
Licensed User
Longtime User
Ok, i tested it: 'mod' is not highlighted in blue as a keyword, but it works!
The algorithm translated from C unfortunately does not work. Thanks for your help
What is should do is this:
factorize(572)="2^2*11*13" ...for example,
but hangs, probably in an infinite loop ...
 

Myr0n

Active Member
Licensed User
Longtime User
I understand to translate literally, this time the code work 100% as you want but it is not translated literally as your original code.

B4X:
Sub factorize(value As Int) As String
    Dim res As String = ""

    If (is_prime(value)) Then
        res = value
        Return res
    End If
    Dim i As Int = 2
    Do While (value >1 And i<=value)
     
        If Not(is_prime(i)) Then
            i=i+1
            Continue
        End If
        Dim exp As Int
        Do While (value Mod i)=0
            value = value/i
            If (exp > 1) Then
                res=res & i '& "^" & exp 'sprintf(res, "%s%d^%d", res, i, exp);
            Else
                res=res & i'sprintf(res, "%s%d", res, i);
            End If
            If (value > 1) Then res=res & " * " 'sprintf(res, "%s * ", res);
            exp=exp+1
        Loop
        i=i+1
    Loop
    Return res
End Sub

Sub is_prime(value As Int) As Boolean
    Dim i As Int = 2
    For i=2 To Sqrt(value) Step 1
        If (value Mod i)=0 Then
            Return False
        End If
    Next
    Return True
End Sub
 
Last edited:

Myr0n

Active Member
Licensed User
Longtime User
well almost, you can add whatever you want.
 

Myr0n

Active Member
Licensed User
Longtime User
I'm sorry If i missed understand your request.
 
Top