primes

Java implementation of Sieve of Eratosthenes that can go past n = 2^32?

牧云@^-^@ 提交于 2019-12-07 01:43:55
问题 Currently I have this prime generator that is limited to n < 2^32-1. I'm not entirely sure how I could expand the limit further, given the limit of elements in an array. Sieve: public class Main { public static void main(String args[]){ long N = 2000000000; // initially assume all integers are prime boolean[] isPrime = new boolean[N + 1]; for (int i = 2; i <= N; i++) { isPrime[i] = true; } // mark non-primes <= N using Sieve of Eratosthenes for (int i = 2; i*i <= N; i++) { // if i is prime,

I have a new algorithm to find factors or primes in linear time - need verification for this

不问归期 提交于 2019-12-06 22:38:23
问题 I have developed an algorithm to find factors of a given number. Thus it also helps in finding if the given number is a prime number. I feel this is the fastest algorithm for finding factors or prime numbers. This algorithm finds if a give number is prime in time frame of 5*N (where N is the input number). So I hope I can call this a linear time algorithm. How can I verify if this is the fastest algorithm available? Can anybody help in this matter? (faster than GNFS and others known)

Python OverflowError: cannot fit 'long' into an index=sized integer

放肆的年华 提交于 2019-12-06 18:40:32
问题 I want to generate two really large prime numbers using an algorithm I found online and changed slightly. I get this error on line 5: Python OverflowError: cannot fit 'long' into an index=sized integer My code: import math def atkin(end): if end < 2: return [] lng = ((end/2)-1+end%2) **sieve = [True]*(lng+1)** for i in range(int(math.sqrt(end)) >> 1): if not sieve[i]: continue for j in range( (i*(i + 3) << 1) + 3, lng, (i << 1) + 3): sieve[j] = False primes = [2] primes.extend([(i << 1) + 3

C# Sieve of Eratosthenes

偶尔善良 提交于 2019-12-06 17:50:26
I have written this code to find prime numbers, and it works well, but the calculation speeds are incredibly slow..... Am I doing this wrong? I know that I might be really doing this the wrong way, but please help me! Thank you very much! using System; using System.Collections.Generic; namespace Primenumbers { class MainClass { public static void Main (string[] args) { List<int> NoPrime = new List<int>(); for(int x = 2; x < 10000;x++) { for(int y = x * 2;y < 10000;y = y + x) { if(!NoPrime.Contains(y)) { NoPrime.Add(y); } } } for(int z = 2; z < 10000;z++) { if(!NoPrime.Contains(z)) { Console

Prime Numbers Optimization in C [closed]

吃可爱长大的小学妹 提交于 2019-12-06 17:32:17
问题 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 try to print the prime numbers; 2 to 1 million. But nothing printed on the console. Could you check my code? And how can I be this code more optimized?

Why is this Haskell code snippet not infinitely recursive?

风流意气都作罢 提交于 2019-12-06 17:12:50
问题 To help me learn Haskell, I am working through the problems on Project Euler. After solving each problem, I check my solution against the Haskell wiki in an attempt to learn better coding practices. Here is the solution to problem 3: primes = 2 : filter ((==1) . length . primeFactors) [3,5..] primeFactors n = factor n primes where factor n (p:ps) | p*p > n = [n] | n `mod` p == 0 = p : factor (n `div` p) (p:ps) | otherwise = factor n ps problem_3 = last (primeFactors 317584931803) My naive

C# Prime Generator, Maxxing out Bit Array

最后都变了- 提交于 2019-12-06 14:33:37
问题 (C#, prime generator) Heres some code a friend and I were poking around on: public List<int> GetListToTop(int top) { top++; List<int> result = new List<int>(); BitArray primes = new BitArray(top / 2); int root = (int)Math.Sqrt(top); for (int i = 3, count = 3; i <= root; i += 2, count++) { int n = i - count; if (!primes[n]) for (int j = n + i; j < top / 2; j += i) { primes[j] = true; } } if (top >= 2) result.Add(2); for (int i = 0, count = 3; i < primes.Length; i++, count++) { if (!primes[i])

Special Pairs with sum as Prime Number

徘徊边缘 提交于 2019-12-06 14:08:53
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 create a 2-D array of size 451*451 and for all pair we can store whether it is prime or not. Now, the

Check whether an integer (or all elements of a list of integers) be prime

与世无争的帅哥 提交于 2019-12-06 13:14:14
I'm doing a simple Haskell function using recursion. At the moment, this seems to work but, if I enter 2 , it actually comes up as false, which is irritating. I don't think the code is as good as it could be, so, if you have any advice there, that'd be cool too! I'm pretty new to this language! EDIT: Ok, so I understand what a prime number is. For example, I want to be able to check 2, 3, 5, 7, etc and have isPrime return true . And of course if I run the function using 1, 4, 6, 8 etc then it will return false . So, my thinking is that in pseudo code I would need to do as follows: num = 2 ->

Program to find the nth prime number

烈酒焚心 提交于 2019-12-06 12:48:01
问题 I wrote a code in python to find the nth prime number. print("Finds the nth prime number") def prime(n): primes = 1 num = 2 while primes <= n: mod = 1 while mod < (num - 1): ptrue = 'true' if num%(num-mod) == 0: ptrue = 'false' break mod += 1 if ptrue == 'true': primes += 1 return(num) nth = int(input("Enter the value of n: ")) print(prime(nth) The code looked fine to me, but it returns an error when I run it: Traceback (most recent call last): File "C:/Users/AV/Documents/Python/nth Prime.py"