primes

Decompose a number into 2 prime co-factors

房东的猫 提交于 2019-12-04 04:46:31
问题 One of the requirements for Telegram Authentication is decomposing a given number into 2 prime co-factors. In particular P*Q = N, where N < 2^63 How can we find the smaller prime co-factor, such that P < square_root(N) My Suggestions: 1) pre-compute primes from 3 to 2^31.5 , then test if N mod P = 0 2) Find an algorithm to test for primes (but we still have to test N mod P =0 ) Is there an algorithm for primes that is well suited to this case? 回答1: Ugh! I just put this program in and then

Is there a fast, functional prime generator?

假装没事ソ 提交于 2019-12-04 03:41:06
Suppose I've got a natural number n and I want a list (or whatever) of all primes up to n . The classic prime sieve algorithm runs in O(n log n) time and O(n) space -- it's fine for more imperative languages, but requires in-place modification to lists and random access, in a fundamental way. There's a functional version involving priority queues, which is pretty slick -- you can check it out here . This has better space complexity at about O(n / log(n)) (asymptotically better but debatable at practical scales). Unfortunately the time analysis is nasty, but it's very nearly O(n^2) (actually, I

F# using sequence cache correctly

孤人 提交于 2019-12-04 03:29:04
I'm trying to use Seq.cache with a function that I made that returns a sequence of primes up to a number N excluding the number 1. I'm having trouble figuring out how to keep the cached sequence in scope but still use it in my definition. let rec primesNot1 n = {2 .. n} |> Seq.filter (fun i -> (primesNot1 (i / 2) |> Seq.for_all (fun o -> i % o <> 0))) |> Seq.append {2 .. 2} |> Seq.cache Any ideas of how I could use Seq.cache to make this faster? Currently it keeps dropping from scope and is only slowing down performance. Seq.cache caches an IEnumerable<T> instance so that each item in the

Quickly determine if a number is prime in Python for numbers < 1 billion

。_饼干妹妹 提交于 2019-12-04 02:09:41
My current algorithm to check the primality of numbers in python is way to slow for numbers between 10 million and 1 billion. I want it to be improved knowing that I will never get numbers bigger than 1 billion. The context is that I can't get an implementation that is quick enough for solving problem 60 of project Euler: I'm getting the answer to the problem in 75 seconds where I need it in 60 seconds. http://projecteuler.net/index.php?section=problems&id=60 I have very few memory at my disposal so I can't store all the prime numbers below 1 billion. I'm currently using the standard trial

How to optimize this Haskell code summing up the primes in sublinear time?

☆樱花仙子☆ 提交于 2019-12-03 22:46:35
Problem 10 from Project Euler is to find the sum of all the primes below given n . I solved it simply by summing up the primes generated by the sieve of Eratosthenes. Then I came across much more efficient solution by Lucy_Hedgehog (sub-linear!). For n = 2⋅10^9 : Python code (from the quote above) runs in 1.2 seconds in Python 2.7.3. C++ code (mine) runs in about 0.3 seconds (compiled with g++ 4.8.4). I re-implemented the same algorithm in Haskell, since I'm learning it: import Data.List import Data.Map (Map, (!)) import qualified Data.Map as Map problem10 :: Integer -> Integer problem10 n =

Prime numbers program

狂风中的少年 提交于 2019-12-03 22:23:08
问题 I'm currently trying out some questions just to practice my programming skills. ( Not taking it in school or anything yet, self taught ) I came across this problem which required me to read in a number from a given txt file. This number would be N. Now I'm suppose to find the Nth prime number for N <= 10 000. After I find it, I'm suppose to print it out to another txt file. Now for most parts of the question I'm able to understand and devise a method to get N. The problem is that I'm using an

Prime generating number finder not producing correct output

随声附和 提交于 2019-12-03 21:59:31
I'm working on this problem: Consider the divisors of 30: 1,2,3,5,6,10,15,30. It can be seen that for every divisor d of 30, d+30/d is prime. Find the sum of all positive integers n not exceeding 100 000 000 such that for every divisor d of n, d+n/d is prime. and I thought for sure I had it, but alas, it's apparently giving me the wrong answer ( 12094504411074 ). I am fairly sure my sieve of Eratosthenes is working (but maybe not), so I think the problem is somewhere in my algorithm. It seems to get the right answer for n = 30 ( 1+2+6+10+22+30 = 71 - is this correct?), but as numbers get

CUDA - Sieve of Eratosthenes division into parts

≡放荡痞女 提交于 2019-12-03 21:18:18
I'm writing implementation of Sieve of Eratosthenes ( https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes ) on GPU. But no sth like this - http://developer-resource.blogspot.com/2008/07/cuda-sieve-of-eratosthenes.html Method: Creating n-element array with default values 0/1 (0 - prime, 1 - no) and passing it on GPU (I know that it can be done directly in kernel but it's not problem in this moment). Each thread in block checks multiples of a single number. Each block checks in total sqrt(n) possibilities. Each block == different interval. Marking multiples as 1 and passing data back to the host

Prime Factorization Program in Java

邮差的信 提交于 2019-12-03 20:33:44
问题 I am working on a prime factorization program implemented in Java. The goal is to find the largest prime factor of 600851475143 (Project Euler problem 3). I think I have most of it done, but I am getting a few errors. Also my logic seems to be off, in particular the method that I have set up for checking to see if a number is prime. public class PrimeFactor { public static void main(String[] args) { int count = 0; for (int i = 0; i < Math.sqrt(600851475143L); i++) { if (Prime(i) && i % Math

CUDA Primes Generation

こ雲淡風輕ζ 提交于 2019-12-03 15:49:36
My CUDA program stops working(it prints nothing) as data size increases over 260k. Can someone tell me why this is happening? This is my first CUDA program. And if I want bigger primes, how to use datatype larger than long long int on CUDA? The graphics card is GT425M. #include<stdio.h> #include<stdlib.h> #include<cuda.h> #define SIZE 250000 #define BLOCK_NUM 96 #define THREAD_NUM 1024 int data[SIZE]; __global__ static void sieve(int *num,clock_t* time){ const int tid = threadIdx.x; const int bid = blockIdx.x; int tmp=bid*THREAD_NUM+tid; if(tid==0) time[bid] = clock(); while(tmp<SIZE){ int i=1