QB64 Phoenix Edition
Simplify Fractions - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Simplify Fractions (/showthread.php?tid=2700)



Simplify Fractions - SMcNeill - 05-18-2024

As promised with the last example utility:

Code: (Select All)
ReDim PF(0) As Long
ReDim Shared As Long Prime_Factors(1 To 168)
Dim As Long num1, num2
Screen _NewImage(1280, 900, 32)
_Delay .5
_ScreenMove _Middle

Init

Do
Input "Give me the top number of your fraction (from 1 to 1016064. 0 quits) =>"; num1
If num1 <= 0 Or num1 >= 1016064 Then System
Input "Give me the bottom number of your fraction (from 1 to 1016064. 0 quits) =>"; num2
If num2 <= 0 Or num2 >= 1016064 Then System
Simplify num1, num2
Print "Simplied those two values become: "; num1; "/"; num2
Loop



Sub Simplify (num1 As Long, num2 As Long)
ReDim PF(0) As Long
GetPF num1, PF()
For i = 1 To UBound(PF)
If num2 Mod PF(i) = 0 Then
num1 = num1 \ PF(i)
num2 = num2 \ PF(i)
End If
Next
End Sub



Sub GetPF (num, PF() As Long)
ReDim PF(1000) As Long
If num < 1 _Orelse num > 1016064 Then Exit Sub
limit = Int(Sqr(num))
PF(0) = 1
If num > 1 Then
count = 1
Do
factor_on = 0
Do
factor_on = factor_on + 1
temp = Prime_Factors(factor_on)
If IsPrime(num) Then
PF(count) = num
finished = -1
Exit Do
ElseIf num Mod temp = 0 Then
PF(count) = temp
count = count + 1
num = num \ temp
Exit Do
End If
Loop
Loop Until finished
End If
ReDim _Preserve PF(count) As Long
End Sub






Function IsPrime (num)
'to check for any given number less than 1,016,064, we only have to do a maximum of 170 comparisons
'as the max value we ever have to check against is the SQR of our number.
'for example, no value higher than 10 could ever be a factor in 100 and be prime!
'so for numbers less than 1,000,000, all we need do is compare them against factors less than 1,000.
'and there's not that many of them, as you can see below!
If num < 2 _Orelse num > 1016064 Then Exit Function
Static As Long Prime_Array_Init, Factor_Count(1 To 10)
Dim As Long factor_on, i, j, factor
If Prime_Array_Init = 0 Then
Prime_Array_Init = -1
Restore Prime_Factor_Count
For i = 1 To 10: Read Factor_Count(i): Next

End If

IsPrime = -1
For j = 1 To 10 'broken down to give 10 quick exit points so we don't check every value for an answer.
For i = 1 To Factor_Count(j)
factor_on = factor_on + 1
factor = Prime_Factors(factor_on)
If num <= factor Then
Exit Function
Else
If num Mod factor = 0 Then IsPrime = 0: Exit Function
End If
Next
If num < factor * factor Then Exit Function
Next

Exit Function
Prime_Factor_Count: 'each value here represents the number of prime factors in number increments of 100
'for example, there are 25 prime numbers between 1 and 100, 21 primes between 101 and 200, ect...
Data 25,21,16,16,17,14,16,14,15,14
End Function

Sub Init
Restore prime_data:
For i = 1 To 168: Read Prime_Factors(i): Next
Exit Sub
prime_data:
Data 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97: 'factors up to 10,000 (100 ^ 2)
Data 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199: 'up to 44,100 (210 ^ 2)
Data 211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293: 'up to 93,636 (306 ^ 2)
Data 307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397: 'up to 160,000 (400 ^ 2)
Data 401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499: 'up to 252,004 (502 ^ 2)
Data 503,509,521,523,541,547,557,563,569,571,577,587,593,599: 'up to 360,000 (600 ^ 2)
Data 601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691: 'up to 490,000 (700 ^ 2)
Data 701,709,719,727,733,739,743,751,757,761,769,773,787,797: 'up to 652,864 (808 ^ 2)
Data 809,811,821,823,827,829,839,853,857,859,863,877,881,883,887: 'up to 820,836 (906 ^ 2)
Data 907,911,919,929,937,941,947,953,967,971,977,983,991,997: 'up to 1,01,064 (1008 ^ 2)


End Sub

Give this two numbers, such as 12 and 16 (to represent 12/16, without me having to parse to get those values), and it simplifies them down to the smallest representation you can have with those numbers. (3/4, in this case.)