primes

Count number of non-prime pairs that when multiplied form a given number N,

倾然丶 夕夏残阳落幕 提交于 2019-12-22 01:31:42
问题 A non-prime pair which forms N is 2 different non-prime numbers where the product of the numbers is N. 1<=N<=10^6 For example For N = 24 there are 2 good pairs (non-prime pairs that form N) (4,6), (1,24), but (2,12), (3,8) are not good. Note: for any 2 numbers a and b pair(a,b) = pair(b,a). There is another condition which states that if the number is a special number, so output = -1 otherwise count the number of non-primes. Number is called special number if it can be represented as a

Finding the 10001st Prime Number - Project Euler [closed]

旧街凉风 提交于 2019-12-22 01:10:27
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I am trying to find the 10,001th prime number. I have looked at other code people have written , but I don't really understand what it means. I have

Special Pairs with sum as Prime Number

怎甘沉沦 提交于 2019-12-22 00:15:54
问题 A number N is given in the range 1 <= N <= 10^50 . A function F(x) is defined as the sum of all digits of a number x. We have to find the count of number of special pairs (x, y) such that: 1. 0 <= x, y <= N 2. F(x) + F(y) is prime in nature We have to count (x, y) and (y, x) only once. Print the output modulo 1000000000 + 7 My approach: Since the maximum value of sum of digits in given range can be 450 (If all the characters are 9 in a number of length 50, which gives 9*50 = 450 ). So, we can

Sieve of Eratosthenes with Wheel Factorization

早过忘川 提交于 2019-12-21 20:53:09
问题 i'm implementing a reasonably fast prime number generator and i obtained some nice results with a few optimizations on the sieve of eratosthenes. In particular, during the preliminary part of the algorithm, i skip all multiples of 2 and 3 in this way: template<class Sieve, class SizeT> void PrimeGenerator<Sieve, SizeT>::factorize() { SizeT c = 2; m_sieve[2] = 1; m_sieve[3] = 1; for (SizeT i=5; i<m_size; i += c, c = 6 - c) m_sieve[i] = 1; } Here m_sieve is a boolean array according to the

Project Euler 3 - Why does this method work?

旧城冷巷雨未停 提交于 2019-12-21 17:32:41
问题 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? I solved this problem on Project Euler my own way, which was slow, and then I found this solution on someone's github account. I can't figure out why it works. Why are a number of factors removed, equal to an index? Any insight? def Euler3(n=600851475143): for i in range(2,100000): while n % i == 0: n //= i if n == 1 or n == i: return i 回答1: This function works by finding successive

Generate primes from 1 to n, crashing for n > 300 million

∥☆過路亽.° 提交于 2019-12-21 17:32:27
问题 Any suggestions as to how I can get this program to work for n = 1 trillion (aside from making upgrades / buying a new computer)? The error is as follows: after building, the program being executing (the command line style output window pops up) and then quickly closes out, and I get the following error "ProjectPrimes.exe has stopped working (Windows is looking for a solution to this problem." I suspect this has to do with memory issues as I first encountered it at n = 20 million but that was

Improving a prime sieve algorithm

Deadly 提交于 2019-12-21 13:08:19
问题 I'm trying to make a decent Java program that generates the primes from 1 to N (mainly for Project Euler problems). At the moment, my algorithm is as follows: Initialise an array of booleans (or a bitarray if N is sufficiently large) so they're all false, and an array of ints to store the primes found. Set an integer, s equal to the lowest prime, (ie 2) While s is <= sqrt(N) Set all multiples of s (starting at s^2) to true in the array/bitarray. Find the next smallest index in the array

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

Why is this prime sieve implementation slower?

我的未来我决定 提交于 2019-12-21 12:18:32
问题 I was just experimenting a bit with (for me) a new programming language: clojure. And I wrote a quite naive 'sieve' implementation, which I then tried to optimise a bit. Strangely enough though (for me at least), the new implementation wasn't faster, but much slower... Can anybody provide some insight in why this is so much slower? I'm also interested in other tips in how to improve this algorithm... Best regards, Arnaud Gouder ; naive sieve. (defn sieve ([max] (sieve max (range 2 max) 2)) (

F# using sequence cache correctly

北城以北 提交于 2019-12-21 09:14:08
问题 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