How to evaluate a math expression given in string form?

With JDK1.6, you can use the built-in Javascript engine. import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; public class Test { public static void main(String[] args) throws ScriptException { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); String foo = “40+2”; System.out.println(engine.eval(foo)); } }

Fastest way to list all primes below N

Warning: timeit results may vary due to differences in hardware or version of Python. Below is a script which compares a number of implementations: ambi_sieve_plain, rwh_primes, rwh_primes1, rwh_primes2, sieveOfAtkin, sieveOfEratosthenes, sundaram3, sieve_wheel_30, ambi_sieve (requires numpy) primesfrom3to (requires numpy) primesfrom2to (requires numpy) Many thanks to stephan for bringing sieve_wheel_30 to my attention. Credit goes to Robert … Read more

Fastest way to determine if an integer is between two integers (inclusive) with known sets of values

There’s an old trick to do this with only one comparison/branch. Whether it’ll really improve speed may be open to question, and even if it does, it’s probably too little to notice or care about, but when you’re only starting with two comparisons, the chances of a huge improvement are pretty remote. The code looks … Read more

Calculate distance between 2 GPS coordinates

Calculate the distance between two coordinates by latitude and longitude, including a Javascript implementation. West and South locations are negative. Remember minutes and seconds are out of 60 so S31 30′ is -31.50 degrees. Don’t forget to convert degrees to radians. Many languages have this function. Or its a simple calculation: radians = degrees * … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)