Re: BR Algorithms Corner
Posted: 14 Jan 2015 23:50
In fact Shor's algorithm is the first one for which he won the Nevanlinna prize.
Consortium of Indian Defence Websites
https://forums.bharat-rakshak.com/
The structure you are describing is actually similar to how hash tables are implemented in several environmentsmatrimc wrote: Think of a pointer array sqrt(n) long where each ement of the array points to a at most sqrt(n) long array. Each insertion can be done in O(sqrt(n)), O(1) to jump to the array into which the input number has to be inserted, O(sqrt(n)) to make space and insert. Since there are n input keys, one ends up with a O(n^1.5) for all insertions. This will also beat the O(n^2) algorithm quite handily and is almost as simple.
Amber G. wrote:FWIW: This may be a question for algorithm dhaga.. but a VERY slight modification, even using brute force, can make the search thousands, -(if not millions) of time faster) .than it seems from above...chilarai wrote:yeah i did a quick and dirty one .its brute force search.
Added later , I could have made it about 6 times faster by having a<= b and b<= c , as of now all combinations are tested 6 times ( the number of permutations )Code: Select all
limit = 1000 l = [1..limit] is2power :: Integer -> Bool is2power = \x -> x `elem` ( map (2^) [0..30]) isfine :: Integer -> Integer -> Integer -> Bool isfine a b c = is2power ( a * b - c) && is2power (b * c - a) && is2power (c * a - b) res = [ ( a , b, c ) | a <- l , b <- l , c <- l , isfine a b c ]
(What I mean is that , (say to for limit=1000) the function is2power is called, about 3 times in each iteration
of 1000*1000*1000 times (well divide by 6 if you use a<=b<=c etc). Even considering is2power function is super fast, the process is hopeless for, say limit=1000000, because you are looking at 10^18 calls..A VERY slight change (How? - can others suggest how) can reduce this by a very huge factor
Code: Select all
import Data.Bits
import Control.Monad
limit = 10000
l = [1..limit]
is2power :: Integer -> Bool
is2power x = ( x >= 1) && ( x .&. (x-1) == 0 )
isfine :: Integer -> Integer -> Integer -> Bool
isfine a b c = is2power ( a * b - c) && is2power (b * c - a) && is2power (c * a - b)
-- in the above can skip check for a*b -c because now it is fine by the construction
res = do
a <- [1..limit]
b <- [1..limit]
guard (a <= b )
c <- filter ( \x-> x>=1 && x <= limit) $ map (\x -> a* b - x ) $ takeWhile (< a*b) $ map (2^) [0..20]
guard (b <= c)
guard $ isfine a b c
return (a,b,c)
main = do
putStrLn $ show res
chilarai wrote:Thanks Amber G, yeah that helps. would be implementing it sometime. My aim to write the program was to see if i can find any patterns to the solutions and then try prove a generic solution by going backwards. But that does not seem likely with only the few ( and likely they are the only ) solutions found . So probably have to return back to algebraic manipulations .
added later : thanks again. With the modification, it could do 10,000 in less than 20 minutes. and the results are the ones already found
[(2,2,3),(2,2,2),(2,6,11),(3,5,7)]
<code omitted here> .
NICE!!chilarai wrote:I have yet another angle ( solution less ) , hope its not a repeat of something already discussed. sorry for my difference in notation .. of a , b and c for the numbers and i,j and k for the powers
so we have
ab - c = 2^i
bc -a = 2^j
re writing the two we have
b.a - c = 2^i
-a + b.c = 2^j
what we have is a two variable equation in a and c , ..solving for a and c we get
a = (b.2^i + 2^j) / ( b^2 -1)
c = (2^i + b.2^j)/(b^2 -1)
i wonder if we can use this somehow in the 3rd equation ca - b = 2^j to have any more insight .
Yes, my program was virtually identical..(though written in Rexx).. and was able to check numbers up to trillions..Amber G. wrote:My method was quick and dirty (took me about 5 minutes to write and debug the code, and employed only the most basic logic) .. I am sure you will find out this can be done, so at present I am not posting my code but rest assured it is quite "simple" once you think about it..
Code: Select all
import Data.Bits
import Control.Monad
limit = 1000000
powerlimit = ceiling $ logBase 2 (fromIntegral (limit * limit))
is2power :: Integer -> Bool
is2power x = ( x >= 1) && ( x .&. (x-1) == 0 )
res = do
b <- [2..limit]
j <- [0..powerlimit]
i <- [0..j]
let (a,ra) = quotRem num den
-- num = b * 2^i + 2^j
num = shift b i + shift 2 (j-1)
den = b*b -1
guard (ra == 0)
guard ( a <= b && a > 1)
let (c,rc) = quotRem num2 den2
-- num2 = 2^i + b*2^j
num2 = shift 2 (i-1) + shift b j
den2 = b*b -1
guard (rc == 0)
guard ( b <= c)
guard (is2power (a * c - b))
return( a, b, c)
main = do
putStrLn $ show res
Code: Select all
#!/usr/bin/python
# Monte carlo simulation for pi by Armen Tanzarian.
import random
def run_simulation(num_samples):
count = 0
for i in range(num_samples):
x = random.random() # Gets a number in range 0..1
y = random.random() # Gets a number in range 0..1
# See if this point falls within the circle or not.
if (x ** 2 + y ** 2) <= 1:
count += 1
return count
num_samples = 1000
points_in_circle = run_simulation(num_samples)
print "pi = ", (4.0 * points_in_circle / num_samples)
Code: Select all
pi = 3.168
pi = 3.236
pi = 3.104
...
Code: Select all
pi = 3.1472
pi = 3.1436
pi = 3.1556
...
Code: Select all
pi = 3.142464
pi = 3.141408
pi = 3.141644
...
sounds like a specialization of the general algorithm.But they also present a new method for applying their general algorithm to specific problems, which yields huge efficiency gains — several orders of magnitude.
http://news.mit.edu/2015/faster-optimiz ... rithm-1023