Have it now - but no chance to optimize the program for beeing fast enough to solve it in less a minute! Even when I made a limit to search only the solutions below 99999Trond wrote:Problem 60 looks fun, I will take a shot at it when I get home.

Oops - searching below 50000 (even 40000Trond wrote:I searched for solutions under 5000, but there weren't any. But there's not way it's going to take less than a minute.
Hi Mike,mike74 wrote:Hi Michael,
I probably won't code a solution in real code but I think the way I would solve problem 88 is:
1.) Start at the lowest value that could possibly be a minimal product-sum number for the given k.
2.) Factor that value in every way possible.
3.) If for any of the factorizations the value is equal to the sum of the factors plus k minus the number of factors, you have found the minimal product-sum number for the given k.
Is it possible you have miscalculated the sum of the minimal product-sum numbers by not removing duplicates? In the problem statement it says "...note that 8 is only counted once in the sum."
By the way, I think http://projecteuler.net/index.php?section=view&id=88 might work better for link to the problem description.
Mike
Code: Select all
#MaxPrimeCache=3000
Global Dim PrimeCache.q(#MaxPrimeCache)
Global PrimeCacheCount
Procedure IsPrime(n.q)
Protected i=1
Protected d=2
Protected Root=Sqr(n)
If n=1
ProcedureReturn #False
Else
While d<=Root
If n%d=0
ProcedureReturn #False
EndIf
If i<PrimeCacheCount
i+1
d=PrimeCache(i)
Else
d+2
EndIf
Wend
ProcedureReturn #True
EndIf
EndProcedure
Procedure InitPrimeCache()
Protected p.q
PrimeCache(1)=2
PrimeCacheCount=1
p=1
Repeat
p+2
If IsPrime(p)
PrimeCacheCount+1
PrimeCache(PrimeCacheCount)=p
EndIf
Until PrimeCacheCount=#MaxPrimeCache
EndProcedure
InitPrimeCache()
Code: Select all
#MaxSumProds=12000
Global NotFound=#MaxSumProds
Global Dim Best(#MaxSumProds)
#MaxNumber=25000
#MaxFacts=15
Global Dim Factor(#MaxNumber,#MaxFacts)
Global Dim Group(#MaxFacts)
Global Dim Solution.b(#MaxNumber)
Global Product
Global Result.q
Procedure Factors()
Protected n,m
Protected p
Protected i,j
Factor(1,0)=1
Factor(1,1)=1
For m=2 To #MaxNumber
n=m
If IsPrime(n)
Factor(n,0)=1
Factor(n,1)=n
Else
i=1
j=0
Repeat
p=PrimeCache(i)
If n%p=0
n/p
j+1
Factor(m,j)=p
;If j>Overflow
; Overflow=j
; Debug -j
;EndIf
Else
i+1
EndIf
Until n=1
Factor(m,0)=j
EndIf
Next m
EndProcedure
Procedure GroupIt(n,g,s,z)
Protected i
Protected d.s
Protected sg,ss,st
If n
If Group(n)=0
Group(n)=g
s+1
If s=Factor(z,0)
s=Factor(z,0)
If Group(s)>1; mindestens zwei Faktoren...
d=#TAB$
For i=1 To s
;d+Str(Factor(z,i))+":"+Str(Group(i))+" "
If Group(i)<>sg
sg=Group(i)
ss+st
If st : d+Str(St)+#TAB$ : EndIf
st=Factor(z,i)
Else
st*Factor(z,i)
EndIf
Next i
ss+st
If st : d+Str(St)+#TAB$ : EndIf
If ss<=Product
ss=Product-ss+Group(s)
If ss<=#MaxSumProds
If Best(ss)=0
NotFound-1
Best(ss)=z
Debug Str(z)+#TAB$+Str(ss)+d
If Solution(z)=#False
Solution(z)=#True
Result+z
EndIf
EndIf
EndIf
EndIf
EndIf
ElseIf n<Factor(z,0)
GroupIt(n+1,g,s,z)
Group(n+1)=0
GroupIt(n+1,g+1,s,z)
Group(n+1)=0
EndIf
EndIf
Else
For i=1 To Factor(z,0)
Group(i)=0
Next i
For i=1 To Factor(z,0)
GroupIt(i,1,0,z)
Next i
EndIf
EndProcedure
Code: Select all
Factors()
Result=0
n=1
Repeat
n+1
f=Factor(n,0)
If f>1
Product=1
For i=1 To f
Product*Factor(n,i)
Next i
GroupIt(0,0,0,n)
EndIf
Until NotFound=1; 2,3,4,...n (keine Lösung für 1)
debug Result
Code: Select all
#MaxPrimeCache=100000
Global prevRes = 0
Global Dim PrimeCache.q(#MaxPrimeCache)
Global PrimeCacheCount
Procedure IsPrime(n.q)
Protected i=1
Protected d=2
Protected Root=Sqr(n)
If n=1
ProcedureReturn #FALSE
Else
While d<=Root
If n%d=0
ProcedureReturn #FALSE
EndIf
If i<PrimeCacheCount
i+1
d=PrimeCache(i)
Else
d+2
EndIf
Wend
ProcedureReturn #TRUE
EndIf
EndProcedure
Procedure InitPrimeCache()
Protected p.q
PrimeCache(1)=2
PrimeCacheCount=1
p=1
Repeat
p+2
If IsPrime(p)
PrimeCacheCount+1
PrimeCache(PrimeCacheCount)=p
EndIf
Until PrimeCacheCount=#MaxPrimeCache
EndProcedure
InitPrimeCache()
#MaxSumProds=12000
Global NotFound=#MaxSumProds
Global lastnf
#MaxNumber=100000
#MaxFacts=30
Global Dim Factor(#MaxNumber,#MaxFacts)
Global Dim Done(#MaxNumber, #MaxFacts)
Global Dim Group(#MaxFacts)
Global Dim Solution.b(#MaxNumber)
Global Product
Global Result.q
Procedure Factors()
Protected n,m
Protected p
Protected i,j
Factor(1,0)=1
Factor(1,1)=1
For m=2 To 100000
n=m
If IsPrime(n)
Factor(n,0)=1
Factor(n,1)=n
Else
i=1
j=0
Repeat
p=PrimeCache(i)
If n%p=0
n/p
j+1
Factor(m,j)=p
;If j>Overflow
; Overflow=j
; Debug -j
;EndIf
Else
i+1
EndIf
Until n=1
Factor(m,0)=j
EndIf
Next m
EndProcedure
Procedure GroupIt(n,g,s,z)
Protected i
Protected d.s
Protected sg,ss,st
If n
If Group(n)=0
Group(n)=g
s+1
factCount = 0
If s=Factor(z,0)
If lastnf = NotFound
d=#TAB$
For i=1 To s
If Group(i)<>sg
sg=Group(i)
ss+st
If st : d+Str(St)+#TAB$ : factCount + 1
EndIf
st=Factor(z,i)
Else
st*Factor(z,i)
EndIf
Next i
ss+st
If st : d+Str(St)+#TAB$ : factCount + 1
EndIf
If Product=ss+prevRes-factCount
NotFound-1
If Solution(z)=#FALSE
Solution(z)=#TRUE
Debug Str(prevRes)+","+Str(z)+","+d
Result+z
Debug Result
EndIf
EndIf
EndIf
ElseIf n<Factor(z,0)
GroupIt(n+1,g,s,z)
Group(n+1)=0
GroupIt(n+1,g+1,s,z)
Group(n+1)=0
EndIf
EndIf
Else
For i=1 To Factor(z,0)
Group(i)=0
Next i
For i=1 To Factor(z,0)
GroupIt(i,1,0,z)
Next i
EndIf
EndProcedure
Factors()
Result=0
n=1
Repeat
lastnf = notFound
n+1 : I = n
prevRes = n
Repeat
I + 1
f=Factor(I,0)
If f>1 Or I < 5
Product = I
GroupIt(0,0,0,I)
Else
Product = I
EndIf
Until lastnf > notFound
Until n = 12000
MessageRequester("answer", Str(Result))
Hi Francis,Dreamland Fantasy wrote:Does anyone have any tips on how to (easily? :roll: ) use massive numbers in PureBasic?
I've written my own add, multiply and power routines (using strings), but they are painfully slow with the more complex problems and to be honest a little crude.
I read elsewhere in the forum that there was a big number library being produced, but that may have just been for 64-bit arithmetic before it was added natively to PureBasic.