primes

Program that checks if a number is prime number

杀马特。学长 韩版系。学妹 提交于 2019-11-26 18:41:07
问题 Hello I have created this program to check if a number is a prime number. It works but for some reason says that 999 is a prime number. Where is my mistake. It would be great if someone explained. Thank You! Here is my program: number = raw_input('Enter a Number: ') nnumber = int(number) prime_range = range(2, nnumber) for x in prime_range: if nnumber % x == 0: print 'Not a Prime Number!' break else: print 'Prime Number!' break 回答1: Trace it. x starts with 2 , then tests 999 % 2 ; it is 1 ,

Project Euler #3 takes forever in Java

被刻印的时光 ゝ 提交于 2019-11-26 18:37:51
问题 Problem #3 on Project Euler is: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? My solution takes forever. I think I got the right implementation; however, when testing with the big number, I have not being able to see the results. It runs forever. I wonder if there's something wrong with my algorithm: public class LargestPrimeFactor3 { public static void main(String[] args) { long start, end, totalTime; long num = 600851475143L;

Fastest primality test

人盡茶涼 提交于 2019-11-26 18:31:10
问题 Could you suggest a fast, deterministic method that is usable in practice, for testing if a large number is prime or not? Also, I would like to know how to use non-deterministic primality tests correctly. For example, if I'm using such a method, I can be sure that a number is not prime if the output is "no", but what about the other case, when the output is "probably"? Do I have to test for primality manually in this case? Thanks in advance. 回答1: The only deterministic, polynomial-time

Sieve of Eratosthenes - Primes between X and N

[亡魂溺海] 提交于 2019-11-26 17:52:43
问题 I found this highly optimised implementation of the Sieve of Eratosthenes for Python on Stack Overflow. I have a rough idea of what it's doing but I must admit the details of it's workings elude me. I would still like to use it for a little project (I'm aware there are libraries to do this but I would like to use this function). Here's the original: ''' Sieve of Eratosthenes Implementation by Robert William Hanks https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below

Conditional tests in primality by trial division

ぐ巨炮叔叔 提交于 2019-11-26 17:24:46
问题 My question is about the conditional test in trial division. There seems to be some debate on what conditional test to employ. Let's look at the code for this from RosettaCode. int is_prime(unsigned int n) { unsigned int p; if (!(n & 1) || n < 2 ) return n == 2; /* comparing p*p <= n can overflow */ for (p = 3; p <= n/p; p += 2) if (!(n % p)) return 0; return 1; } Wheel factorization or using a predetermined list of primes will not change the essence of my question. There are three cases I

Print series of prime numbers in python

心不动则不痛 提交于 2019-11-26 16:26:41
I am trying to learn Python programming, and I'm pretty new at this. I was having issues in printing a series of prime numbers from one to hundred. I can't figure our what's wrong with my code. Here's what I wrote; it prints all the odd numbers instead of primes: for num in range(1,101): for i in range(2,num): if (num%i==0): break else: print(num) break Igor Chubin You need to check all numbers from 2 to n-1 (to sqrt(n) actually, but ok, let it be n). If n is divisible by any of the numbers, it is not prime. If a number is prime, print it. for num in range(2,101): prime = True for i in range(2

Parallel Algorithms for Generating Prime Numbers (possibly using Hadoop's map reduce)

亡梦爱人 提交于 2019-11-26 16:17:21
问题 Generating Prime numbers is a toy problem that I often attempt from time to time, especially when experimenting with a new programming language, platform or style. I was thinking of attempting to write a Prime Number Generation algorithm or a Prime Number Test Algorithm using Hadoop (Map Reduce). I thought I'd post this question to get tips, references, to algorithms, approaches. Although my primary interest is a Map Reduce based algorithm I wouldn't mind looking at new Hadoop programming

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

橙三吉。 提交于 2019-11-26 15:49:19
问题 I have been trying to write Sieve of Eratosthenes algorithm in JavaScript. Basically I just literally followed the steps below: Create a list of consecutive integers from 2 to (n-1) Let first prime number p equal 2 Starting from p, count up in increments of p and removes each of these numbers (p and multiples of p) Go to the next number in the list and repeat 2,3,4 Add unintentionally deleted prime numbers back to the list and this is what I have come up with: function eratosthenes(n){ var

Finding largest prime number out of 600851475143?

会有一股神秘感。 提交于 2019-11-26 15:32:59
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; // largest prime value project_3 object = new project_3(); for(long x=1L; x <= 600851475143L; x++) { if

C#: How to make Sieve of Atkin incremental

空扰寡人 提交于 2019-11-26 14:25:45
问题 I don't know if this is possible or not, but I just gotta ask. My mathematical and algorithmic skills are kind of failing me here :P The thing is I now have this class that generates prime numbers up to a certain limit: public class Atkin : IEnumerable<ulong> { private readonly List<ulong> primes; private readonly ulong limit; public Atkin(ulong limit) { this.limit = limit; primes = new List<ulong>(); } private void FindPrimes() { var isPrime = new bool[limit + 1]; var sqrt = Math.Sqrt(limit)