primes

Calculating phi(k) for 1<k<N

大城市里の小女人 提交于 2019-11-27 18:25:06
Given a large N, I need to iterate through all phi(k) such that 1 < k < N : time-complexity must be O(N logN) memory-complexity must be sub O(N) (since the values of N will be around 10 12 ) Is it possible? If so, how? RBarryYoung This can be done with Memory complexity O(Sqrt(N)) and CPU complexity O(N * Log(Log(N))) with an optimized windowed Sieve of Eratosthenes, as implemented in the code example below. As no language was specified and as I do not know Python, I have implemented it in VB.net, however I can convert it to C# if you need that. Imports System.Math Public Class

How does this prime number test in Java work?

为君一笑 提交于 2019-11-27 18:19:32
问题 The code snippet below checks whether a given number is a prime number. Can someone explain to me why this works? This code was on a study guide given to us for a Java exam. public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " +

Reason for 5381 number in DJB hash function?

允我心安 提交于 2019-11-27 17:23:44
Can anyone tell me why the number 5381 is used in DJB hash function ? DJB Hash function is h(0) = 5381 h(i) = 33 * h(i-1) ^ str[i] A c program: unsigned int DJBHash(char* str, unsigned int len) { unsigned int hash = 5381; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = ((hash << 5) + hash) + (*str); } return hash; } 5381 is just a number that, in testing, resulted in fewer collisions and better avalanching . You'll find "magic constants" in just about every hash algo. Mark Johnson I stumbled across a comment that sheds some light on what DJB is up to: /* * DJBX33A (Daniel J.

Finding the nth prime number using Python

三世轮回 提交于 2019-11-27 16:58:21
问题 When I run this code, even for just counting to the 10th prime number (instead of 1000) I get a skewed/jacked output--all "not prime" titles for my is_composite variable, my test_num is giving me prime and composite numbers, and my prime_count is off Some of the answers that developers have shared use functions and the math import--that's something we haven't yet covered. I am not trying to get the most efficient answer; I am just trying to write workable python code to understand the basics

Python prime generator in one-line

前提是你 提交于 2019-11-27 16:56:47
问题 I'm trying to create prime number generator in one-line of Python just as a fun exercise. The following code works as expected, but it is too slow: primes = lambda q: (i for i in xrange(1,q) if i not in [j*k for j in xrange(1,i) for k in xrange(1,i)]) for i in primes(10): print i, So I I tried to do it by only checking up to the square-root of j and k: primes = lambda q: (i for i in xrange(1,q) if i not in [j*k for j in xrange(1,int(round(math.sqrt(i)+1))) for k in xrange(1,int(round(math

Program that checks if a number is prime number

痞子三分冷 提交于 2019-11-27 16:32:11
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 Trace it. x starts with 2 , then tests 999 % 2 ; it is 1 , so else is executed, "Prime number!" is printed, and loop is broken out of. Program ends. Instead, you need

Project Euler #3 takes forever in Java

独自空忆成欢 提交于 2019-11-27 16:16:07
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; long pFactor = 0; start = System.currentTimeMillis(); for(int i = 2; i < num; i++) { if(isPrime(i)) { if

Fastest primality test

自古美人都是妖i 提交于 2019-11-27 15:29:32
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. templatetypedef The only deterministic, polynomial-time algorithm for primality testing I know of is the AKS primality test ( http://en.wikipedia.org/wiki

Why is this scala prime generation so slow/memory intensive?

我是研究僧i 提交于 2019-11-27 15:14:19
问题 I run out of memory while finding the 10,001th prime number. object Euler0007 { def from(n: Int): Stream[Int] = n #:: from(n + 1) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.filter(_ % s.head != 0)) def primes = sieve(from(2)) def main(args: Array[String]): Unit = { println(primes(10001)) } } Is this because after each "iteration" (is this the correct term in this context?) of primes , I increase the stack of functions to be called to get the next element by one? One solution

What is wrong with my isPrime method?

倖福魔咒の 提交于 2019-11-27 14:10:53
问题 This is my isPrime method: private static boolean isPrime(int num) { if (num % 2 == 0) return false; for (int i = 3; i * i < num; i += 2) if (num % i == 0) return false; return true; } I put isPrime(9) and it returns true . What is wrong with the method? 回答1: Your condition should be i * i <= num private static boolean isPrime(int num) { if (num == 2) return true; if (num < 2 || num % 2 == 0) return false; for (int i = 3; i * i <= num; i += 2) if (num % i == 0) return false; return true; }