number-theory

Represent a prime number as a sum of four squared integers

别说谁变了你拦得住时间么 提交于 2019-12-06 14:50:25
问题 Given a prime number p, find a four integers such that p is equal to sum of square of those integers. 1 < p < 10^12. If p is of form 8n + 1 or 8n + 5, then p can be written as sum of two squares. This can be solved in O(sqrt(p)*log(sqrt(p)). But for other cases,i.e. when p cannot be written as sum of two squares, than is very inefficient. So, it would be great if anyone can give some resource material which i can read to solve the problem. 回答1: Given your constraints, I think that you can do

Converting from a string to a number

人走茶凉 提交于 2019-12-06 12:07:55
So, I am trying to write a program to decode 6-character base-64 numbers. Here is the problem statement: Return the 36-bit number represented as a base-64 number in reverse order by the 6-character string s where the order of the 64 numerals is: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-+ i.e. decode('000000') → 0 decode('gR1iC9') → 9876543210 decode('++++++') → 68719476735 I would like to do this WITHOUT strings. The easiest way to do this would be to create the inverse of the following function: def get_digit(d): ''' Convert a base 64 digit to the desired character '''

Miller-Rabin Primality test in Java

落花浮王杯 提交于 2019-12-06 08:48:11
问题 I am currently working on Project Euler and thought that it might be more interesting (and a better learning experience) if don't just brute force all of the questions. On question 3 it asks for prime factors of a number and my solution will be to factor the number (using another factoring algorithm) and then test the factors for primality. I came up with this code for a Miller-Rabin Primality test (after thoroughly researching primality test) and it returns true for all the composite odd

Getting a list of square-free numbers

本小妞迷上赌 提交于 2019-12-05 19:04:31
One way to get that is for the natural numbers (1,.., n ) we factorise each and see if they have any repeated prime factors, but that would take a lot of time for large n . So is there any better way to get the square-free numbers from 1,.., n ? Armen Tsirunyan You could use Eratosthenes Sieve's modified version: Take a bool array 1.. n ; Precalc all squares that are less than n; that's O(sqrt(N)); For each square and its multiples make the bool array entry false... From http://mathworld.wolfram.com/Squarefree.html There is no known polynomial time algorithm for recognizing squarefree integers

Represent a prime number as a sum of four squared integers

做~自己de王妃 提交于 2019-12-04 21:17:42
Given a prime number p, find a four integers such that p is equal to sum of square of those integers. 1 < p < 10^12. If p is of form 8n + 1 or 8n + 5, then p can be written as sum of two squares. This can be solved in O(sqrt(p)*log(sqrt(p)). But for other cases,i.e. when p cannot be written as sum of two squares, than is very inefficient. So, it would be great if anyone can give some resource material which i can read to solve the problem. Given your constraints, I think that you can do a smart brute force. First, note that if p = a^2 + b^2 + c^2 + d^2, each of a, b, c, d have to be less than

Miller-Rabin Primality test in Java

本小妞迷上赌 提交于 2019-12-04 12:51:16
I am currently working on Project Euler and thought that it might be more interesting (and a better learning experience) if don't just brute force all of the questions. On question 3 it asks for prime factors of a number and my solution will be to factor the number (using another factoring algorithm) and then test the factors for primality. I came up with this code for a Miller-Rabin Primality test (after thoroughly researching primality test) and it returns true for all the composite odd number I have put in. Can anybody help me to figure out why? I thought I had coded the algorithm correctly

What is the fastest way to check if two given numbers are coprime?

こ雲淡風輕ζ 提交于 2019-12-04 10:43:27
问题 One way is to calculate their gcd and check if it is 1. Is there some faster way? 回答1: The Euclidean algorithm (computes gcd ) is very fast. When two numbers are drawn uniformly at random from [1, n] , the average number of steps to compute their gcd is O(log n) . The average computation time required for each step is quadratic in the number of digits. There are alternatives that perform somewhat better (i.e., each step is subquadratic in the number of digits), but they are only effective on

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

痴心易碎 提交于 2019-12-04 05:21:37
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 like Goldbach's_conjecture more. How can I solve this problem? This problem has a simple trick. You

Efficient algorithm for finding a common divisor closest to some value?

二次信任 提交于 2019-12-04 00:42:22
问题 I have two numbers, x1 and x2 . For a number y , I want to calculate the common divisor of x1 and x2 as close as possible to y . Is there an efficient algorithm for this? I believe it's time to rephrase my problem and be more clear . This is not about integers... So, say we have two numbers x1 and x2 . Say, the user inputs a number y . What I want to find, is a number y' close to y so that x1 % y' and x2 % y' are very small (smaller than 0.02 , for example, but lets call this number LIMIT ).

Approach and Code for o(log n) solution

こ雲淡風輕ζ 提交于 2019-12-03 22:01:05
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 for cases in range(test): arr=[int(x) for x in input().split()] N=arr[0] mod=arr[1] answer = reduce