number-theory

Recombine Number to Equal Math Formula

Hello, I've been thinking about a math/algorithm problem and would appreciate your input on how to solve it! If I have a number (e.g. 479), I would like to recombine its digits or combination of them to a math formula that matches the original number. All digits should be used in their original order, but may be combined to numbers (he...

Algorithm to determine non-negative-values solution existance for linear diophantine equation

Hello, I am looking for a method to determine if there is a solution to equations such as: 3n1+4n2+5n3=456, where n1,n2,n3 are positive integers. Or more general: are there zero or positive integers n1,n2,n3... that solves the equation k1n1+k2n2+k3n3...=m where k1,k2,k3... and m are known positive integers. I don't need to find a solut...

Calculating sum of geometric series (mod m)

I have a series S = i^(m) + i^(2m) + ............... + i^(km) (mod m) 0 <= i < m, k may be very large (up to 100,000,000), m <= 300000 I want to find the sum. I cannot apply the Geometric Progression (GP) formula because then result will have denominator and then I will have to find modular inverse which may not exist (if the ...

How can I optimize my C / x86 code?

int lcm_old(int a, int b) { int n; for(n=1;;n++) if(n%a == 0 && n%b == 0) return n; } int lcm(int a,int b) { int n = 0; __asm { lstart: inc n; mov eax, n; mov edx, 0; idiv a; mov eax, 0; cmp eax, edx; jne lstart; mov eax, n; mo...

What's a nice method to factor gaussian integers?

I already have prime factorization (for integers), but now I want to implement it for gaussian integers but how should I do it? thanks! ...

Finding perfect numbers (optimization)

I coded up a program in C# to find perfect numbers within a certain range as part of a programming challenge . However, I realized it is very slow when calculating perfect numbers upwards of 10000. Are there any methods of optimization that exist for finding perfect numbers? My code is as follows: using System; using System.Collections....

Greatest GCD between some numbers

Hi, We've got some nonnegative numbers. We want to find the pair with maximum gcd. actually this maximum is more important than the pair! For example if we have: 2 4 5 15 gcd(2,4)=2 gcd(2,5)=1 gcd(2,15)=1 gcd(4,5)=1 gcd(4,15)=1 gcd(5,15)=5 the answer is 5. ...

Sieve of Eratosthenes in Haskell

Hi, I'm solving some classic problems in Haskell to develop my functional skills and I have a problem to implement an optimization suggested at http://programmingpraxis.com/2009/02/19/sieve-of-eratosthenes/ I have three "solutions" to this problem and the third one is too slow compared to the second solution. Can someone suggest some...

Fastest way to modify one digit of an integer

Suppose I have an int x = 54897, old digit index (0 based), and the new value for that digit. What's the fastest way to get the new value? Example x = 54897 index = 3 value = 2 y = f(x, index, value) // => 54827 Edit: by fastest, I definitely mean faster performance. No string processing. ...

How to improve number theory related skills ?

I know it is a very general question, but i'm not sure how to get started and improve number theory related skills.Any good way and good book recommendations for starters and intermediates (specially related to ACM ICPC and for other programming competitions) ...