primes

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

杀马特。学长 韩版系。学妹 提交于 2019-12-17 10:41:55
问题 Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Eratosthenes method, implemented it, and changed the question title and tags appropriately. Thanks to everyone who helped! Introduction I wrote this fancy little method that generates an array of int containing the prime numbers less than the specified upper bound. It works very well, but I have a concern. The Method private static int []

Fast Prime Number Generation in Clojure

大憨熊 提交于 2019-12-17 10:24:54
问题 I've been working on solving Project Euler problems in Clojure to get better, and I've already run into prime number generation a couple of times. My problem is that it is just taking way too long. I was hoping someone could help me find an efficient way to do this in a Clojure-y way. When I fist did this, I brute-forced it. That was easy to do. But calculating 10001 prime numbers took 2 minutes this way on a Xeon 2.33GHz, too long for the rules, and too long in general. Here was the

Fastest-in term of space- way to find prime numbers with python

ⅰ亾dé卋堺 提交于 2019-12-17 07:41:19
问题 Maybe it is a stupid question, but i was wondering if you could provide the shortest source to find prime numbers with Python. I was also wondering how to find prime numbers by using map() or filter() functions. Thank you (: EDIT: When i say fastest/shortest I mean the way with the less characters/words. Do not consider a competition, anyway: i was wondering if it was possible a one line source, without removing indentation always used with for cycles. EDIT 2:The problem was not thought for

New state of the art in unlimited generation of Hamming sequence

北城余情 提交于 2019-12-17 07:34:49
问题 (this is exciting!) I know, the subject matter is well known. The state of the art (in Haskell as well as other languages) for efficient generation of unbounded increasing sequence of Hamming numbers, without duplicates and without omissions, has long been the following (AFAIK - and btw it is equivalent to the original Edsger Dijkstra's code too): hamm :: [Integer] hamm = 1 : map (2*) hamm `union` map (3*) hamm `union` map (5*) hamm where union a@(x:xs) b@(y:ys) = case compare x y of LT -> x

Fastest algorithm for primality test [closed]

流过昼夜 提交于 2019-12-17 07:16:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I need to test primality on intervals between numbers which are really big (in the range of long long), so i need some fast algorithm for checking if a number is prime or not. Please suggest your ideas. 回答1: One good method is the Miller-Rabin test. It should be noted however, that this is only a probabilistic

double stream feed to prevent unneeded memoization?

假如想象 提交于 2019-12-17 05:14:14
问题 I'm new to Haskell and I'm trying to implement Euler's Sieve in stream processing style. When I checked the Haskell Wiki page about prime numbers, I found some mysterious optimization technique for streams. In 3.8 Linear merging of that wiki: primesLME = 2 : ([3,5..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) where primes' = 3 : ([5,7..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) joinL ((x:xs):t) = x : union xs (joinL t) And it says “ The double primes feed is introduced here to

double stream feed to prevent unneeded memoization?

丶灬走出姿态 提交于 2019-12-17 05:13:32
问题 I'm new to Haskell and I'm trying to implement Euler's Sieve in stream processing style. When I checked the Haskell Wiki page about prime numbers, I found some mysterious optimization technique for streams. In 3.8 Linear merging of that wiki: primesLME = 2 : ([3,5..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) where primes' = 3 : ([5,7..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) joinL ((x:xs):t) = x : union xs (joinL t) And it says “ The double primes feed is introduced here to

Generating integers in ascending order using a set of prime numbers

…衆ロ難τιáo~ 提交于 2019-12-17 05:06:06
问题 I have a set of prime numbers and I have to generate integers using only those prime factors in increasing order. For example, if the set is p = {2, 5} then my integers should be 1, 2, 4, 5, 8, 10, 16, 20, 25, … Is there any efficient algorithm to solve this problem? 回答1: The basic idea is that 1 is a member of the set, and for each member of the set n so also 2 n and 5 n are members of the set. Thus, you begin by outputting 1, and push 2 and 5 onto a priority queue. Then, you repeatedly pop

Finding largest prime number out of 600851475143?

六月ゝ 毕业季﹏ 提交于 2019-12-17 03:19:27
问题 I'm trying to solve problem 3 from http://projecteuler.net. However, when I run thing program nothing prints out. What am I doing wrong? Problem: What is the largest prime factor of the number 600851475143 ? public class project_3 { public boolean prime(long x) // if x is prime return true { boolean bool = false; for(long count=1L; count<x; count++) { if( x%count==0 ) { bool = false; break; } else { bool = true; } } return bool; } public static void main(String[] args) { long ultprime = 0L; /

How to determine if a number is a prime with regex?

南笙酒味 提交于 2019-12-17 02:39:06
问题 I found the following code example for Java on RosettaCode: public static boolean prime(int n) { return !new String(new char[n]).matches(".?|(..+?)\\1+"); } I don't know Java in particular but understand all aspects of this snippet except for the regex itself I have basic to basic-advanced knowledge of Regex as you find it in the built-in PHP functions How does .?|(..+?)\\1+ match prime numbers? 回答1: You said you understand this part, but just to emphasize, the String generated has a length