## 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 = powers.map{|i| (0..i).to_a} divisors = exponents.shift.product(*exponents).map do |powers| primes.zip(powers).map{|prime, power| prime ** … Read more

## Best library for statistics in C++? [closed]

Check the links on mathtools.net. The page for statistics libraries for C++ has links. Another page http://www.thefreecountry.com/sourcecode/mathematics.shtml lists few more. Have you checked the ‘R project’? I think you can call ‘R objects’ from C/C++.

## 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

## How can I safely average two unsigned ints in C++?

Your last approach seems promising. You can improve on that by manually considering the lowest bits of a and b: unsigned int average = (a / 2) + (b / 2) + (a & b & 1); This gives the correct results in case both a and b are odd.

## 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

## How to round a integer to the close hundred?

Try the Math.Round method. Here’s how: Math.Round(76d / 100d, 0) * 100; Math.Round(121d / 100d, 0) * 100; Math.Round(9660d / 100d, 0) * 100;

## 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

## How does this bitwise operation check for a power of 2?

Any power of 2 minus 1 is all ones: (2 N – 1 = 111….b) 2 = 2^1. 2-1 = 1 (1b) 4 = 2^2. 4-1 = 3 (11b) 8 = 2^3. 8-1 = 7 (111b) Take 8 for example. 1000 & 0111 = 0000 So that expression tests if a number is NOT a … 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

## What’s the simplest way to extend a numpy array in 2 dimensions?

The shortest in terms of lines of code i can think of is for the first question. >>> import numpy as np >>> p = np.array([[1,2],[3,4]]) >>> p = np.append(p, [[5,6]], 0) >>> p = np.append(p, [[7],[8],[9]],1) >>> p array([[1, 2, 7], [3, 4, 8], [5, 6, 9]]) And the for the second question p … Read more