I added here the sample code, but this kind of “factorization” looks difficult…
testh.m (1.9 KB)
It looks little faster though when each case is checked only once as in below code:
testh2.m (1.4 KB)
A little different method:
testh3.m (1.6 KB)
It still might not be best possible way to do it.
The question slowness looks coming from generating the product vector…
Searching the vector for the result is still relatively faster.
How to generate the product matrix for this (assuming that it is the good way to solve this)?
And for example if I set
k=[-1 0 1];
and I want repeat k n times in new vector m, how to make it?
m=[k k k k];
but let’s say k is repeated n times instead of 4 times as in above case.
Code looks operating relatively fast and gives results as:
Give real number (0.1-0.99999999): 0.95
Calculating "prime factors"...
2^-15*3^-10*5^6*7^6=0.9500485160703729≈0.95
[a,b,c,d]=[-15,-10,6,6]
log(0.95)=-0.0222764≈-15*log(2)-10*log(3)+6*log(5)+6*log(7)=-0.0222542
Give real number (0.1-0.99999999): 0.32
Calculating "prime factors"...
2^3*3^0*5^-2*7^0=0.32≈0.32
[a,b,c,d]=[3,0,-2,0]
log(0.32)=-0.49485≈3*log(2)-2*log(5)=-0.49485
Looping the search algorithm an use same product vector:
testh4.m (1.7 KB)
Possibly some type of bug, because for 100 I get as:
Give real number (0.1-0.99999999, 0=quit): 100
2^6*3^-17*5^1*7^9=99.99332461737717≈100
[a,b,c,d]=[6,-17,1,9]
log(100)=2≈6*log(2)-17*log(3)+1*log(5)+9*log(7)=1.99997
while 0.01 seems to work:
Give real number (0.1-0.99999999, 0=quit): 0.01
2^-2*3^0*5^-2*7^0=0.01≈0.01
[a,b,c,d]=[-2,0,-2,0]
log(0.01)=-2≈-2*log(2)-2*log(5)=-2
Bug fixed (The array elements where overwritten during generation):
testh5.m (1.7 KB)
It might still occur in some other conditions though or not.