primes

Sum of prime numbers below 2,000,000 giving wrong result

微笑、不失礼 提交于 2019-12-02 00:05:17
问题 I have to sum all prime numbers below 2,000,000 but my code is giving the wrong result(1,179,908,154 right is 142,913,828,922), since it's working perfectly with lower values I can't figure out what's wrong. #include <iostream> using namespace std; int main(){ unsigned int j, i=2,ans=2, interval=2000000; while(i<=interval){ i++; j=2; while(i!=j){ if(i % j != 0) j++; else{ i++; j=2;} } if (i>=interval) break; cout << i<< endl; ans+=i; } cout << ans; cin.get(); return 0; } 回答1: You are

R: Split data frame when number of columns is a prime

喜欢而已 提交于 2019-12-01 23:40:50
I have a data.frame that has 131 columns. I need to part this into groups of about 10 to 15 variables (i.e., splitting by column, not by row!). Obviously, as 131 is a prime number, not all the new dataframes can be of equal length... I searched for an answer in the posts How to cut data in even pieces in R? Split a vector into chunks in R Splitting a large vector into intervals in R But they all seem to assume that the new data frames are of equal size. EDIT thanks to the comments below, I will try to clarify: My data frame looks like this head(trainData) ID drop_vce_Range drop_dat_Range blck

Is there a way to “grow” my array as my program continues?

不羁的心 提交于 2019-12-01 22:52:27
I am creating a prime generator in c++ using an array to store primes as I find them and then use them to check against potentials later. Is there a way to "grow" my array as my program continues? See std:vector. http://new.cplusplus.com/reference/stl/vector/ I would use a std:vector something like this: vector<int> primes; then you would add primes to it by using: primes.push_back(2); and retrieve values from the vector by using: primes[0]; Good luck! You could use a std::vector template, and use the reserve method based on an upper-bound for the prime counting function . It sounds like you

Decompose a number into 2 prime co-factors

杀马特。学长 韩版系。学妹 提交于 2019-12-01 21:37:31
One of the requirements for Telegram Authentication is decomposing a given number into 2 prime co-factors. In particular P*Q = N, where N < 2^63 How can we find the smaller prime co-factor, such that P < square_root(N) My Suggestions: 1) pre-compute primes from 3 to 2^31.5 , then test if N mod P = 0 2) Find an algorithm to test for primes (but we still have to test N mod P =0 ) Is there an algorithm for primes that is well suited to this case? Ugh! I just put this program in and then realized you had tagged your question C#. This is C++, a version of Pollard Rho I wrote a couple years ago and

What is the bottleneck in this primes related predicate?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 21:13:44
问题 So here it is : I'm trying to calculate the sum of all primes below two millions (for this problem), but my program is very slow. I do know that the algorithm in itself is terribly bad and a brute force one, but it seems way slower than it should to me. Here I limit the search to 20,000 so that the result isn't waited too long. I don't think that this predicate is difficult to understand but I'll explain it anyway : I calculate the list of all the primes below 20,000 and then sum them. The

Prime numbers generator explanation? [duplicate]

旧巷老猫 提交于 2019-12-01 18:02:20
This question already has an answer here: Sieve of Eratosthenes - Finding Primes Python 15 answers I was searching for an algorithm to generate prime numbers. I found the following one done by Robert William Hanks. It is very efficient and better than the other algorithms but I can not understand the math behind it. def primes(n): """ Returns a list of primes < n """ lis = [True] * n for i in range(3,int(n**0.5)+1,2): if lis[i]: lis[i*i::2*i]=[False]*int((n-i*i-1)/(2*i)+1) return [2] + [i for i in range(3,n,2) if lis[i]] What is the relation between the array of True s values and the final

Prime numbers generator explanation? [duplicate]

柔情痞子 提交于 2019-12-01 16:54:28
问题 This question already has answers here : Sieve of Eratosthenes - Finding Primes Python (15 answers) Closed 2 years ago . I was searching for an algorithm to generate prime numbers. I found the following one done by Robert William Hanks. It is very efficient and better than the other algorithms but I can not understand the math behind it. def primes(n): """ Returns a list of primes < n """ lis = [True] * n for i in range(3,int(n**0.5)+1,2): if lis[i]: lis[i*i::2*i]=[False]*int((n-i*i-1)/(2*i)

Overflow while using recur in clojure

若如初见. 提交于 2019-12-01 15:48:23
I have a simple prime number calculator in clojure (an inefficient algorithm, but I'm just trying to understand the behavior of recur for now). The code is: (defn divisible [x,y] (= 0 (mod x y))) (defn naive-primes [primes candidates] (if (seq candidates) (recur (conj primes (first candidates)) (remove (fn [x] (divisible x (first candidates))) candidates)) primes) ) This works as long as I am not trying to find too many numbers. For example (print (sort (naive-primes [] (range 2 2000)))) works. For anything requiring more recursion, I get an overflow error. (print (sort (naive-primes [] (range

Overflow while using recur in clojure

隐身守侯 提交于 2019-12-01 15:43:17
问题 I have a simple prime number calculator in clojure (an inefficient algorithm, but I'm just trying to understand the behavior of recur for now). The code is: (defn divisible [x,y] (= 0 (mod x y))) (defn naive-primes [primes candidates] (if (seq candidates) (recur (conj primes (first candidates)) (remove (fn [x] (divisible x (first candidates))) candidates)) primes) ) This works as long as I am not trying to find too many numbers. For example (print (sort (naive-primes [] (range 2 2000))))

C++ Prime Numbers program [closed]

ぐ巨炮叔叔 提交于 2019-12-01 14:50:39
I'm working on a C++ program that determines and prints the prime numbers between 3 and an integer 'x' the user inputs. I'm assuming that I need a double nested loop for this, one to iterate from 3 to x and the other to check if the number is prime. I think it needs to do something like go from 2 to x-1? I'm just really not sure how to do this syntax-wise. Thanks for any help! :) EDIT: This is what I have: #include <iostream> #include <cmath> using std::cout; using std::endl; using std::cin; int main(){ int x; int i; int j; cout << "Please enter an integer 'x' greater than 3: " << endl; cin >>