primes

What is a sensible prime for hashcode calculation?

拟墨画扇 提交于 2019-11-26 23:38:33
Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would generate for example (slightly shortened:) class HashTest { int i; int j; public int hashCode() { final int prime = 31; int result = prime + i; result = prime * result + j; return result; } } (If you have more attributes in the class, result = prime * result + attribute.hashCode(); is repeated for each additional attribute. For ints .hashCode() can be omitted.) This seems fine but for the choice 31 for the prime. It is probably taken from the hashCode implementation of Java String , which was used for

How can I test for primality?

风流意气都作罢 提交于 2019-11-26 22:51:07
问题 I am writing a little library with some prime number related methods. As I've done the groundwork (aka working methods) and now I'm looking for some optimization. Ofcourse the internet is an excellent place to do so. I've, however, stumbled upon a rounding problem and I was wondering how to solve this. In the loop I use to test a number for it's primality it's more efficient to search until sqrt(n) instead of n/2 or even n - 1. But due to rounding problems some number get skipped and thus

The Sieve of Atkin

非 Y 不嫁゛ 提交于 2019-11-26 22:33:43
I have been trying to learn algorithms for generating prime numbers and I came across Sieve of Atkin on Wikipedia. I understand almost all parts of the algorithm, except a few. Here are the questions: How are the three quadratic equations below formed? 4x^2+y^2, 3x^2+y^2 and 3x^2-y2 The algorithm in wikipedia talks about modulo sixty but I dont understand how/where that is used in the psudocode below. How are these reminders 1,5,7 and 11 found? Below is the pseudocode from Wikipedia for reference: // arbitrary search limit limit ← 1000000 // initialize the sieve for i in [5, limit]: is_prime(i

Find prime numbers using Scala. Help me to improve

≯℡__Kan透↙ 提交于 2019-11-26 21:39:41
问题 I wrote this code to find the prime numbers less than the given number i in scala. def findPrime(i : Int) : List[Int] = i match { case 2 => List(2) case _ => { val primeList = findPrime(i-1) if(isPrime(i, primeList)) i :: primeList else primeList } } def isPrime(num : Int, prePrimes : List[Int]) : Boolean = prePrimes.forall(num % _ != 0) But, I got a feeling the findPrime function, especially this part: case _ => { val primeList = findPrime(i-1) if(isPrime(i, primeList)) i :: primeList else

Generating integers in ascending order using a set of prime numbers

这一生的挚爱 提交于 2019-11-26 20:48:43
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? 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 the front item of the priority queue, output it if it is different from the previous output, and push 2

Generate a list of primes up to a certain number

纵饮孤独 提交于 2019-11-26 20:17:42
I'm trying to generate a list of primes below 1 billion. I'm trying this, but this kind of structure is pretty shitty. Any suggestions? a <- 1:1000000000 d <- 0 b <- for (i in a) {for (j in 1:i) {if (i %% j !=0) {d <- c(d,i)}}} This is an implementation of the Sieve of Eratosthenes algorithm in R. sieve <- function(n) { n <- as.integer(n) if(n > 1e6) stop("n too large") primes <- rep(TRUE, n) primes[1] <- FALSE last.prime <- 2L for(i in last.prime:floor(sqrt(n))) { primes[seq.int(2L*last.prime, n, last.prime)] <- FALSE last.prime <- last.prime + min(which(primes[(last.prime+1):n])) } which

Printing prime numbers from 1 through 100

こ雲淡風輕ζ 提交于 2019-11-26 19:54:24
This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97. But I don't think that's the way my book wants it to be written. It mentions something about square root of a number. So I did try changing my 2nd loop to for (int j=2; j<sqrt(i); j++) but it did not give me the result I needed. How would I need to change this code to the way my book wants it to be? int main () { for (int i=2; i<100; i++) for (int j=2; j<i; j++) { if (i % j == 0) break; else if (i == j+1) cout << i << " "; } return 0; } A prime integer number is one that

Speed up bitstring/bit operations in Python?

有些话、适合烂在心里 提交于 2019-11-26 19:43:51
I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def prime_numbers(limit=1000000): '''Prime number generator. Yields the series 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ... using Sieve of Eratosthenes. ''' yield 2 sub_limit = int(limit**0.5) flags = [False, False] + [True] * (limit - 2) # flags = BitString(limit) # Step through all the odd numbers for i in range(3, limit, 2): if flags[i] is False: # if flags[i] is True: continue

Find n primes after a given prime number, without using any function that checks for primality

╄→гoц情女王★ 提交于 2019-11-26 19:07:43
How to write a Program to find n primes after a given number? e.g. first 10 primes after 100, or first 25 primes after 1000. Edited: below is what I tried. I am getting output that way, but can we do it without using any primality-testing function? #include<stdio.h> #include<conio.h> int isprime(int); main() { int count=0,i; for(i=100;1<2;i++) { if(isprime(i)) { printf("%d\n",i); count++; if(count==5) break; } } getch(); } int isprime(int i) { int c=0,n; for(n=1;n<=i/2;n++) { if(i%n==0) c++; } if(c==1) return 1; else return 0; } Sure. Read about the Sieve of Eratosthenes . Instead of checking

Check if number is prime number

给你一囗甜甜゛ 提交于 2019-11-26 18:45:45
I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 == 0 || num1 == 1) { Console.WriteLine(num1 + " is not prime number"); Console.ReadLine(); } else { for (int a = 2; a <= num1 / 2; a++) { if (num1 % a == 0) { Console.WriteLine(num1 + " is not prime number"); return; } } Console.WriteLine(num1 + " is a prime number"); Console.ReadLine(); } Soner Gönül var number; Console.WriteLine("Accept number:"); number