All factors of a given number

In Ruby, the prime library gives you the factorization: require ‘prime’ 4800.prime_division #=> [[2, 6], [3, 1], [5, 2]] To get that list of yours, you take the cartesian product of the possible powers: require ‘prime’ def factors_of(number) primes, powers = number.prime_division.transpose exponents ={|i| (0..i).to_a} divisors = exponents.shift.product(*exponents).map do |powers|{|prime, power| prime ** … Read more

Calculate angle between two Latitude/Longitude points

using this referance to calculate Angle: private double angleFromCoordinate(double lat1, double long1, double lat2, double long2) { double dLon = (long2 – long1); double y = Math.sin(dLon) * Math.cos(lat2); double x = Math.cos(lat1) * Math.sin(lat2) – Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon); double brng = Math.atan2(y, x); brng = Math.toDegrees(brng); brng = (brng + 360) % … Read more

parseFloat rounding

Use toFixed() to round num to 2 decimal digits using the traditional rounding method. It will round 4.050000000000001 to 4.05. num.toFixed(2); You might prefer using toPrecision(), which will strip any resulting trailing zeros. Example: 1.35+1.35+1.35 => 4.050000000000001 (1.35+1.35+1.35).toFixed(2) => 4.05 (1.35+1.35+1.35).toPrecision(3) => 4.05 // or… (1.35+1.35+1.35).toFixed(4) => 4.0500 (1.35+1.35+1.35).toPrecision(4) => 4.05 Reference: JavaScript Number Format … Read more

Convert Linear scale to Logarithmic

Notation As is the convention both in mathematics and programming, the “log” function is taken to be base-e. The “exp” function is the exponential function. Remember that these functions are inverses we take the functions as: exp : ℝ → ℝ+, and log : ℝ+ → ℝ. Solution You’re just solving a simple equation here: … Read more

Cracking short RSA keys

Your teacher gave you: Public Key: (10142789312725007, 5) which means n = 10142789312725007 e = 5 where n is the modulus and e is the public exponent. In addition, you’re given Private Key: (10142789312725007, 8114231289041741) meaning that d = 8114231289041741 where d is the decryption exponent that should remain secret. You can “break” RSA by … Read more