number-theory

algorithm to find products of a set of primes, in order, greater than x

若如初见. 提交于 2019-12-24 08:18:29
问题 Consider the finite set {2,3,5,...,n}. I am interested in primes but the question could apply to any set of numbers. I want to find all possible products of these numbers in ascending order, and in particular greater than or equal to some number x. Does anyone know a nice algorithm for this? EDIT to clarify: Each factor in the input set may be used any number of times. If the input were {2,3,5,7} the output would be {2,3,4,5,6,7,8,9,10,12,14,15,16,18,...}. The algorithm can stop as soon as it

How to define matching axis notches from existing “step list”

匆匆过客 提交于 2019-12-24 02:09:30
问题 I need a way to align tick marks on two separate axis, while being able to control the "step" value (value between tick marks), where both axis start at mark 0 and end on a different maximum value. Why this problem: Flot, the JS charting package has an option to align tick marks, but when I do, I cannot control the step value. I can however control the step value directly, but then I lose the ability to align tick marks. I can however revert to defining my own max and step values, to get what

How to find total number of divisors upto N?

安稳与你 提交于 2019-12-23 03:32:14
问题 Given a number N, have to find number the divisors for all i where i>=1 and i<=N. Can't figure it out.Do I have to this using prime factorization? Limit is N<=10^9 Sample Output: 1 --> 1 2 --> 3 3 --> 5 4 --> 8 5 --> 10 6 --> 14 7 --> 16 8 --> 20 9 --> 23 10 --> 27 11 --> 29 12 --> 35 13 --> 37 14 --> 41 15 --> 45 回答1: To compute much faster, use the following Pseudocode: sum = 0; u = floor(sqrt(N)); foreach k <= u, sum += Floor(N / K); sum = 2*sum-u*u The above formula is given by Peter

how to represent a number as a sum of 4 primes?

给你一囗甜甜゛ 提交于 2019-12-21 12:59:02
问题 Here is the problem (Summation of Four Primes) states that : The input contains one integer number N (N<=10000000) in every line. This is the number you will have to express as a summation of four primes Sample Input: 24 36 46 Sample Output: 3 11 3 7 3 7 13 13 11 11 17 7 This idea comes to my mind at a first glance Find all primes below N Find length of list (.length = 4) with Integer Partition problem (Knapsack) but complexity is very bad for this algorithm I think. This problem also looks

How does this code find the number of trailing zeros from any base number factorial?

左心房为你撑大大i 提交于 2019-12-21 09:25:46
问题 The code below works perfectly but I would like someone to explain to me the mathematics behind it. Basically, how does it work? #include <stdio.h> #include <stdlib.h> /* atoi */ #define min(x, y) (((x) < (y)) ? (x) : (y)) int main(int argc, char* argv[]) { const int base = 16; int n,i,j,p,c,noz,k; n = 7; /* 7! = decimal 5040 or 0x13B0 - 1 trailing zero */ noz = n; j = base; /* Why do we start from 2 */ for (i=2; i <= base; i++) { if (j % i == 0) { p = 0; /* What is p? */ while (j % i== 0) {

Approach and Code for o(log n) solution

天涯浪子 提交于 2019-12-21 06:45:25
问题 f(N) = 0^0 + 1^1 + 2^2 + 3^3 + 4^4 + ... + N^N. I want to calculate ( f(N) mod M ). These are the constraints. 1 ≤ N ≤ 10^9 1 ≤ M ≤ 10^3 Here is my code test=int(input()) ans = 0 for cases in range(test): arr=[int(x) for x in input().split()] N=arr[0] mod=arr[1] #ret=sum([int(y**y) for y in range(N+1)]) #ans=ret for i in range(1,N+1): ans = (ans + pow(i,i,mod))%mod print (ans) I tried another approach but in vain. Here is code for that from functools import reduce test=int(input()) answer=0

Calculating 1^X + 2^X + … + N^X mod 1000000007

徘徊边缘 提交于 2019-12-20 17:30:12
问题 Is there any algorithm to calculate (1^x + 2^x + 3^x + ... + n^x) mod 1000000007 ? Note: a^b is the b-th power of a. The constraints are 1 <= n <= 10^16, 1 <= x <= 1000 . So the value of N is very large. I can only solve for O(m log m) if m = 1000000007 . It is very slow because the time limit is 2 secs. Do you have any efficient algorithm? There was a comment that it might be duplicate of this question, but it is definitely different. 回答1: You can sum up the series 1**X + 2**X + ... + N**X

How to compute a^^b mod m?

痞子三分冷 提交于 2019-12-18 11:33:51
问题 I have to compute efficiently a^^b mod m for large values of a,b,m<2^32 where ^^ is the tetration operator: 2^^4=2^(2^(2^2)) m is not a prime number and not a power of ten. Can you help? 回答1: To be clear, a^^b is not the same thing as a^b, it is the exponential tower a^(a^(a^...^a)) where there are b copies of a, also known as tetration. Let T(a,b) = a^^b so T(a,1) = a and T(a,b) = a^T(a,b-1). To compute T(a,b) mod m = a^T(a,b-1) mod m, we want to compute a power of a mod m with an extremely

Easiest way to perform modular matrix inversion with Python?

筅森魡賤 提交于 2019-12-18 11:21:36
问题 I'd like to take the modular inverse of a matrix like [[1,2],[3,4]] mod 7 in Python. I've looked at numpy (which does matrix inversion but not modular matrix inversion) and I saw a few number theory packages online, but nothing that seems to do this relatively common procedure (at least, it seems relatively common to me). By the way, the inverse of the above matrix is [[5,1],[5,3]] (mod 7). I'd like Python to do it for me though. 回答1: A hackish trick which works when rounding errors aren't an

Calculating sum of geometric series (mod m)

喜你入骨 提交于 2019-12-17 23:31:12
问题 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 denominator and m are not coprime). So I made an alternate algorithm making an assumption that these powers will make a cycle of length much smaller than k (because it