I've run this code in ideone: #include <iostream> #include <math.h> using namespace std; bool is_perfect_square(unsigned long long int n); bool is_pentagonal(unsigned long long int n); int main() { // I was just verifying that your functions are correct /* for (int i=0; i<100; i++) { cout << "Number " <<...

modulus,number-theory,modular-arithmetic

There two important theories that would help you to solve this problem :- Modular Exponentiation Fermat's little theorem Modular Exponentiation :- This simple recursive formula to make you understand :- (a^n)%p = ((a^(n-1))%p*a)%p The above formula can help you prevent the overflow which occurs if a^n is large. Moreover fast...

cryptography,number-theory,elgamal

When you have: x * invX = 1 mod p the following equality is generally not true: (g ^ x) ^ invX = g mod p Above expression means multiplying g*g*....*g a certain number of times, x * invX, which is also k * p + 1 according to first...

math,proof,prime-factoring,number-theory

Initially the process proceeds to search for the smallest factor of n smaller than its square root. If it doesn't have one, then n is prime. So print out n as it's one smallest prime factor! If you find a smallest factor then it must be prime. If not, it's...

c++,performance,number-theory,integer-arithmetic,modular-arithmetic

You can satisfy for one i at a time while preserving all the ones already satisfied: step is the LCM of all the a[i] already satisfied (initially 1). y is the minimum value satisfying all the ones you did already. The key concept is that any Y satisfying all i...

java,biginteger,number-theory,modular-arithmetic

Flustered this was put on hold... For the sake of theory, if you wanted to write your own custom method please check the following out, based on a math trick to avoid the computation. First the solution, and then the math behind it. Your subroutine could look something like the...

math,factorization,number-theory

You're solving the problem from the wrong end. For any number X = p1^a1 * p2^a2 * ... * pn^an // p1..pn are prime d(X) = (a1 + 1)*(a2 + 1)* ... *(an + 1) For instance 50 = 4 * 25 = 2^2 * 5^2 d(50) = (1 +...

language-agnostic,rsa,number-theory,modular-arithmetic

Well, sort of... Suppose that you have determined the number "d" such that d*e=1 (mod phi(n)) Where phi(n) is the size of the set of relatively prime numbers relative to n. For example, if n=pq where p and q are prime, then phi(n)=(p-1)*(q-1). Then m=c^d (mod n) In the case...

algorithm,math,language-agnostic,number-theory

The way I'd implement something like this is to store the aspect ratios (1:144, 2:72, 3:48, 4:36, etc.) into a sorted array. Then for each incoming image, calculate its aspect ratio, then find the nearest desired ratio using binary search. Even better, store the log of the aspect ratios, and...

python,algorithm,number-theory

A look-up table and bit-twiddling lets you solve this efficiently. ruler = dict((1<<i, i+1) for i in xrange(63)) for i in xrange(1, 20): print ruler[i & ~(i-1)], ...

c++,random,genetic-algorithm,number-theory

For crypto, you need very strong properties on your random numbers. Much of the literature out there focusses on these sorts of requirements. A typical solution would be seeding iterated applications of SHA-256 using environmental noise (hard drive delays, network packets, mouse movements, RDRAND, HAVEGE, ...). For Monte-Carlo simulations or...

You forgot to move the index of the solution array when you recurse. generatePartitions(solution, number, sum, partitions + 1, solution[partitions]); becomes: generatePartitions(solution, number, sum, partitions + 1, solution[partitions-1]); Coliru link: http://coliru.stacked-crooked.com/a/83b0418e1b1c9bc1 Also, I'd propose using some form of std::pair<int,int> and a binary tree of some sort to do this for...

1557 ^ 998 doesn't quite fit into a long int. To be a bit more constructive: If you computed p like so, it would take longer, but avoid the overflow: p = 1; for ( i=0; i<(prime_number-1)/2; i++ ) p = (p*a) % prime_number; There are (very good) ways to...

java,encryption,cryptography,public-key-encryption,number-theory

p doesn't fit into an int, so PrivateFields[0].intValue() must be wrong. Your primitiveRoot algorithm has to take BigInteger and not int.

arrays,algorithm,prime-factoring,number-theory

This problem can be solved by finding a maximum matching in the following graph. For each array value x, for each prime factor of x with multiplicity, make a new vertex. If x = 12, for example, then we create two 2 vertices and one 3 vertex. Make edges between...

algorithm,math,greatest-common-divisor,number-theory,clrs

That pseudocode is not Pollard-rho factorization despite the label. It is one trial of the related Brent's factorization method. In Pollard-rho factorization, in the ith step you compute x_i and x_(2i), and check the GCD of x_(2i)-x_i with n. In Brent's factorization method, you compute GCD(x_(2^a)-x_(2^a+b),n) for b=1,2, ..., 2^a....

Haha, it's easy! I can iterate over all digits, adding each parcel... Using the properties: 1) (a+b) mod c = (a mod c + b mod c) mod c 2) (a*b) mod c = (a mod c * b mod c) mod c The power of ten can be increased...

python-3.x,primes,sieve-of-eratosthenes,number-theory

Always try to measure empirical complexity of your code at several ranges. Your code is slow because of how you find the set difference, and that you always convert between set and list and back. You should be using one set throughout, and updating it in place with sete.difference_update(range(p*p,n,p*2)) To...

haskell,math,ghc,number-theory

The cheap, cheesy way to make this compile is to add FlexibleContexts, then add KnownNat (k1 * k2) to the context of crt. Once I did this, I could successfully call it in ghci: > crt (3 :: Mod Integer 5) (5 :: Mod Integer 7) 33 Have fun working...

algorithm,optimization,language-agnostic,number-theory

an divides all previous elements if and only if it divides the greatest common divisor of those elements. So you need to keep track of gcd(a1, a2, ..., an) and the minimum an for which an | gcd(...)....