primes

Is Swift really slow at dealing with numbers?

十年热恋 提交于 2019-12-03 03:33:33
问题 As I was playing around with a swift tutorial, I started to write a custom isPrime method to check if a given Int is prime or not. After writing it I realized it was working properly but found it a bit slow to perform isPrime on some quite large numbers (still much lower then Int.max ). So I wrote the same piece of code in objc and the code was executed much faster (a factor of 66x). Here is the swift code: class Swift { class func isPrime(n:Int) -> Bool { let sqr : Int = Int(sqrt(Double(n)))

Finding the Nth Twin Prime

女生的网名这么多〃 提交于 2019-12-03 03:10:17
I was trying to solve a problem on SPOJ. We are required to calculate the nth twin prime pair( primes differing by 2). n can be as large as 10^5. I tried a precalculation using a sieve, I had to sieve up to 10^8 to get the maximum n twin prime, but the time limit is strict(2s) and it times out. I noticed people have solved it in 0.00 seconds, so i looked around for a formula on google, and couldnt get anything helpful. Could someone please guide me? Thanks in advance!! So basically, sieving up to 20,000,000 is enough, according to Wolfram Alpha. Use plain sieve of Eratosthenes, on odds, with

Counting coprimes in a sequence

心不动则不痛 提交于 2019-12-03 03:09:52
Having a sequence of n <= 10^6 integers, all not exceeding m <= 3*10^6, I'd like to count how many coprime pairs are in it. Two numbers are coprime if their greatest common divisor is 1. It can be done trivially in O(n^2 log n), but this is obviously way to slow, as the limit suggests something closer to O(n log n). One thing than can be done quickly is factoring out all the numbers, and also throwing out multiple occurences of the same prime in each, but that doesn't lead to any significant improvement. I also thought of counting the opposite - pairs that have a common divisor. It could be

Finding a prime number after a given number

落爺英雄遲暮 提交于 2019-12-03 03:06:14
问题 How can I find the least prime number greater than a given number? For example, given 4, I need 5; given 7, I need 11. I would like to know some ideas on best algorithms to do this. One method that I thought of was generate primes numbers through the Sieve of Eratosthenes, and then find the prime after the given number. 回答1: Some other methods have been suggested and I think that they are good, but it really depends on how much you want to have to store or compute on the spot. For instance if

How do I generate Primes Using 6*k +- 1 rule

梦想与她 提交于 2019-12-03 02:38:40
We know that all primes above 3 can be generated using: 6 * k + 1 6 * k - 1 However we all numbers generated from the above formulas are not prime. For Example: 6 * 6 - 1 = 35 which is clearly divisible by 5. To Eliminate such conditions, I used a Sieve Method and removing the numbers which are factors of the numbers generated from the above formula. Using the facts: A number is said to be prime if it has no prime factors. As we can generate all the prime numbers using the above formulas. If we can remove all the multiples of the above numbers we are left with only prime numbers. To generate

Why is the size 127 (prime) better than 128 for a hash-table?

ε祈祈猫儿з 提交于 2019-12-03 01:28:28
问题 Supposing simple uniform hashing, that being, any given value is equally like to hash into any of the slots of the hash. Why is it better to use a table of size 127 and not 128? I really don't understand what's the problem with the power of 2 numbers. Or how it actually makes any difference at all. When using the division method, we usually avoid certain values of m (table size). For example, m should not be a power of 2, since if m = 2^p , then h(k) is just the p lowest-order bits of k. Let

How to print prime numbers up to the user's entered integer?

被刻印的时光 ゝ 提交于 2019-12-02 22:20:09
问题 Good afternoon everyone, I'm currently trying to create a program that does the following: Develop a code that will print all prime numbers up to the user's entered number. An example of the output: Enter an integer (2 or above): 19 The prime numbers up to you integer are: 2 3 5 7 11 13 17 19 Unfortunately, there is also a list of "requirements" that need to be met as well: If the user enters a number below 2, your program should print a message that the number is not valid, and then stop. A

In haskell how would you go about generating a list of all prime numbers upto a number say x?

限于喜欢 提交于 2019-12-02 21:22:43
问题 so the function would be something like primesearch::Int -> [Int] . For example, primesearch 4 = [2,3,5,7] . Would you need to use the sieve function somehow? or is there another way of doing it? 回答1: To generate the first k prime numbers, or the prime numbers <= n , I recommend a sieve. Which kind of sieve depends on how many primes you want. For small numbers of primes, a monolithic Eratosthenes bit sieve is simple and fast. But if you want large numbers of primes, a monolithic sieve would

Interview question : What is the fastest way to generate prime number recursively? [closed]

浪尽此生 提交于 2019-12-02 21:21:24
Generation of prime number is simple but what is the fastest way to find it and generate( prime numbers) it recursively ? Here is my solution. However, it is not the best way. I think it is O(N*sqrt(N)). Please correct me, if I am wrong. public static boolean isPrime(int n) { if (n < 2) { return false; } else if (n % 2 == 0 & n != 2) { return false; } else { return isPrime(n, (int) Math.sqrt(n)); } } private static boolean isPrime(int n, int i) { if (i < 2) { return true; } else if (n % i == 0) { return false; } else { return isPrime(n, --i); } } public static void generatePrimes(int n){ if(n

How do I generate the first n prime numbers?

痞子三分冷 提交于 2019-12-02 20:25:38
I am learning Ruby and doing some math stuff. One of the things I want to do is generate prime numbers. I want to generate the first ten prime numbers and the first ten only. I have no problem testing a number to see if it is a prime number or not, but was wondering what the best way is to do generate these numbers? I am using the following method to determine if the number is prime: class Integer < Numeric def is_prime? return false if self <= 1 2.upto(Math.sqrt(self).to_i) do |x| return false if self%x == 0 end true end end In Ruby 1.9 there is a Prime class you can use to generate prime