C++ :: Find Prime Numbers From 0 To 100 Million - Running On 8 Cores?

May 11, 2012

I made a program which finds the prime numbers from 0 to 100 million, and ran it on my laptop (core 2 duo, 1.83ghz, 2gb ram). It takes around 35 seconds for it to sieve all the non-prime numbers on my laptop.

I then ran the same program on a windows server, 2x 64 bit Intel Xeon Quad Core Processors @ 1.6 ghz, 6gb ram, and it takes the same amount of time, even when I set the priority to high and the affinity to all 8 cores, it seems to only use one core for the processing, is there any way to make it use all 8 cores at close to 100%?

View 11 Replies


ADVERTISEMENT

C++ :: Find Prime Numbers Between Given Pair Of Numbers And Store Them Into Array?

Apr 18, 2014

Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.

I'm stuck on how to put the prime numbers into an array.

The input file has the numbers 1 & 100.

Here's what I have so far.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");

[Code] .....

View 1 Replies View Related

C :: Find Prime Numbers - How To Use Bit Array

Aug 19, 2013

I wrote some code for class to find prime numbers.The teacher says that I need to revise my code with the requirement below: use a bit array to store the prime number checking. The bit array will also be in the heap. Use the max value of an unsigned 32-bt integer (UINT_MAX) to be the maximum size of the prime number you want to check.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <ctype.h>
#include <stdint.h>

//function to set all non-primes to 0
void *zero_multiples(void *threadid);

[Code] .....

View 4 Replies View Related

C++ :: Find All Prime Numbers In A Range

Oct 17, 2013

I am trying to create a program that lists all prime numbers within a range of two number. Why the below program isn't working?

#include <iostream>
using namespace std;
int main(){
int high_range;
int low_range;
int w;

[Code] .....

View 3 Replies View Related

C++ :: Find All Prime Numbers From 1 To 1000?

Jan 9, 2013

#include<iostream>
#include <cstdlib>
using namespace std;

[Code].....

View 10 Replies View Related

C++ :: Find The Prime Numbers Between Two Values?

Jan 26, 2013

i did a code that determine if the number is prime or not, and i have to do a one that finding the prime numbers between two variables .

the first code:

#include<iostream>
using namespace std;
int main(){

[Code].....

View 4 Replies View Related

C++ :: Find All Prime Numbers Between 1 And 100 Using Sieve Of Eratosthenes

Dec 23, 2014

I had to write a program to find all the primes between 1 and 100 using the sieve of Eratosthenes so I wrote this program :

#include <iostream>
#include <vector>
using namespace std;

// find all the primes number using Eratosthenes
int main() {
vector<bool> numbers(101);

[Code] .....

My problem is that if I try to modify the program to find all the primes between 1 and max (setting the size of the vector uqual to max) my program becomes slower and I would like to listen by other people some possible solution to don't use vector in this program, the author hasn't shown me yet how to delete a value from a vector so I just used this method to solve the problem.

View 3 Replies View Related

C :: Write A Code To Find All The Prime Numbers Before A User Entered Number

Apr 9, 2014

I am trying to write code to find all the prime numbers before a user entered number. I started with a working program and when I tried to create a function, it got all messed up.

Code:

#include <stdio.h>
int is_prime( int num );
int get_positive_integer(void);
int main( ) {
int upper; /* upper limit to check */
int num; /* current number to check */
int isprime;
/* used to flag if number is prime or not */

[Code]...

It says the error is on line 23

View 6 Replies View Related

C :: Calculate Prime Numbers In Range And Return Count Of Prime Numbers

Apr 28, 2013

I wrote a program which sends a starting and ending range to other processes and the processes calculate the prime numbers in that range and return the count of prime numbers to the head process, process 0. But this is not working properly at the moment. I realize I still have to split up the range based on how many processes I have...I still have not figured out how I want to set that up. I

Code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int isPrime(int num);
int main(int argc, char **argv){
}

[code]....

View 7 Replies View Related

C++ :: Prime Number Program Not Working With Odd Non-prime Numbers

Dec 2, 2014

I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones. Here is the code:

Code:
#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;

[Code] ...

View 3 Replies View Related

C/C++ :: Finding All Prime Numbers To Nth Prime Number

Aug 24, 2014

I'm a new coder for C++, and I've recently learned java before starting this programming language. I'm attempting to find all prime numbers up to the number a user enters (i.e. User enters 10, System shows "1,2,3,5,7"),

#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int num;
cout << "Enter a positive number." << endl;

[Code] ....

I've been looking at my forloop, and I can't seem to pinpoint the problem with it.

View 1 Replies View Related

C++ :: Find Highest Prime Of 600851475143

Jun 6, 2013

I have reduced it for lower numbers but for some reason with higher numbers when i get the program to out put all the primes they are in minuses!!!

I need a way to make this even faster, or maybe theres a better mathematical answer, I know that for numbers in their 1,000,000,000,000 have an average of 1 prime every 26.6 numbers (i think thats right there is 37,607,912,018 primes in the number 1,000,000,000,000) is there anyway i can utilize this??

#include <iostream>
using namespace std;
int main() {
int primeanswer = 1,p = 1;
for (int i=1;i<35; i++)

[Code] .....

View 19 Replies View Related

C :: How To Find Biggest Prime Factor Of A Number

Feb 10, 2015

i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug

Code:

#include <stdio.h> // main functions
#include <math.h> // for sqrt function
int main()
{
int i, j, k, f; // F = Flag;
printf("Enter K

[Code]...

View 9 Replies View Related

C++ :: Find Largest Prime Number K Of Array 1

Aug 30, 2013

While finding the primes , I do not know how to make it into one array so that...

View 7 Replies View Related

C/C++ :: Find Prime Factors Of Number Input By User?

Apr 21, 2014

find the prime factors of the number input by the user. This time, we are going to have the user input a number, and the program will find all prime numbers from 2 to that number and prime factors of the number input by the user

#include <iostream>
#include <cmath>
using namespace std;

[Code]......

View 5 Replies View Related

C/C++ :: Find Sum Of Prime Factors Of User Input Integer

Sep 16, 2014

As an assignment for school , I've to write a program to find the sum prime factors of a user input integer.

E.g. 20 = 2 x 2 x 5 , Sum = 2 + 2 + 5 = 9
E.g. 10 = 2 x 5 , Sum = 2 + 5 = 7

My method for finding the result is as follows :

- Divide the number by increasing values if int i , starting from i=2.

- Once I get a value of i that can divide the number without giving me a remainder , I add this value of i to int sum and divide the number by i.

- I will repeat this process until the user input value is equal to 1.

My code is as shown:

#include<stdio.h>
int primecheck(int n); // Function to check if i is prime
int primesum(int n); // Function to sum the values of i that are prime
int main(void) {
int n;
int sum;
printf("Enter a number (> 1): "); //Prompting and scanning user input ,n
scanf("%d",&n);
sum = primesum(n);

[Code] .....

But for some reason I keep getting an incorrect result, it's as if it is missing out the last factor for each case.

Eg. 20 = 2 x 2 x 5 , the result I get is 4 , which is 2+2
Eg. 40 = 2 x 2 x 2 x 5 , the result I get is 6 , which is 2+2+2;

I've looked through my code numerous times.

View 7 Replies View Related

C :: Sum Of Prime Numbers

Feb 21, 2014

Code:

#include<stdio.h>
int main(){

int num,i,count,min,max,sum=0;

printf("Enter min range: ");
scanf("%d",&min);

[code]....

when I ran the program , I got "4" with prime numbers. I only way I was able to remove the "4" was this "num!=4".

View 1 Replies View Related

C :: Prime Numbers And Arrays

May 27, 2013

I was going through a book I have about C trying to learn about arrays, and one of the first few examples of what could been done with an array was showing how to use an array to generate a list of prime numbers. I've been staring at this program for about an hour trying to understand how it works (it does), but the workings of one for loop within the program.

Code:

#include <stdio.h>
#include <stdbool.h>
int main () {
int p, i, primes[50], primeIndex = 2;
bool isPrime;

[Code]...

So that is saying in order for this loop to go on, really two conditions must be met since there's that && operator. isPrime must be true (I think that's what it means by just having "isPrime" and not setting it equal to anything) and p / primes[i] must be greater than or equal to primes[i].So at the beginning of the loop, since i = 1, p = 5 (as per surrounding loop), and prime[i] = 3 ( as per the variable assignment at the beginning of the program ), the loop condition would be "isPrime && 5 / 3 >= 3"

"5 / 3 >= 3" The loop should stop right there! 1.666666667 is NOT greater than or equal to 3!

View 14 Replies View Related

C :: Factorial Using Prime Numbers?

Feb 3, 2014

Code:

#include <stdio.h>
int find_next_prime(int num);
int is_prime(int num);
int main()

[Code]......

How would I go about counting the number of times a factorial has a specific prime number?

For example, 5! = (2^3)*(3^1)*(5^1), 6! = (2^4)*(3^2)*(5^1).

How would I begin to design my find_prime_count function in order to count how many times each occurs? My program is to read in a number between (2<=N<=100) from a text file and output the results exactly like above which I still have to figure out after, I'll assume I have to use fscanf.

View 8 Replies View Related

C++ :: Prime Numbers In A Given Range

Feb 17, 2013

I have an assignment where I have to use two for loops. I need to ask the user for any two numbers and be able to list all the numbers in between and their factors and state whether or not the number is prime or not.

View 2 Replies View Related

C++ :: N Prime Numbers - Print Out And Sum

Mar 3, 2014

Write a c++ program to print out the first n prime numbers and the sum of the first n prime numbers.

View 7 Replies View Related

C/C++ :: Function For Prime Numbers

Mar 9, 2015

I keep getting this error:

||In function 'int main()':|
|19|error: too few arguments to function 'int isPrime(int, int)'|
|13|note: declared here|

Code:

#include <iostream>
#include <fstream>
using namespace std;
int isPrime(int , int);
int main() {
int result = 0;

[Code] ....

View 2 Replies View Related

C/C++ :: Prime Numbers In Calendar?

Sep 25, 2014

The object is to have the user enter in the number of days then the day the calendar would start on. This part I was able to achieve and run okay. Once I wanted to get the values/days that were prime to have a P besides them this is where I ran into trouble. In this case P stands for prime numbers.

The issue I have is that some of the values that are prime have the correct P but others have the P as well such as 9, 15, and 25 - which are not prime numbers.

I was able to create a program that checked for prime numbers separately but I had issues merging them. Below is the merged code followed by the prime checker.

#include <stdio.h>
int main(int argc, const char * argv[]) {
int i, N, Start;
int j, GetNumber, PrimeNumber = 0;

[code].....

View 2 Replies View Related

C/C++ :: Generate Prime Numbers Up To N

Oct 22, 2014

Given a number "n" supplied by the user, write all the prime numbers less than or equal to "n"

not at all how to make the number I enter me for the numbers that I need even a couple makes the prime number if the command is used repeat for but and decision making but the truth is that I can think of no way to do for this exercise is that the prime numbers are those numbers which are divided only by 1 and themselves also

until the moment I have this code

#include <stdio.h>
#include <stdlib.h>
main(){
int n;
n=0;
printf("enter the desired number: ");
scanf("%f",&n);
system("pause");
}

View 5 Replies View Related

C++ :: Finding Divisers Of Non-prime Numbers?

Mar 17, 2013

I have written a program that finds the divisers of non-prime numbers and outputs them. However, the output is only one diviser per line(because of the repetition to find the divbisers), but the instructor wants us to have 5 to 10 divisers per line. How can i write this loop so it will only output the divisers in one line?

View 1 Replies View Related

C++ :: Finding Only Prime Numbers With For Loops

Feb 18, 2013

A prime number is a number greater than 1 which is only evenly divisible by 1 and itself. For this assignment you will find which numbers from 2 to n (where n is a user-specified number) are prime. Ask the user for a number, n, greater than 2. Keep asking for a number until a number greater than 2 is provided. Assume that the user will only enter numbers (that is, you do not need to check if a user enters text).

Use a loop to iterate on a variable, i, from 2 through n. For each iteration, check all numbers from 2 through i to determine whether the number is prime. If it is prime, print out i and the word "Prime". Use the modulus operator, %, to determine if a number is prime

Example output:

Please enter a number larger than two: -939 Please enter a number larger than two: 2 Please enter a number larger than two: 20 2 Prime 3 Prime 5 Prime 7 Prime 11 Prime 13 Prime 17 Prime 19 Prime

This is what I have so far. I suppose i'm not really understanding how to continue with the second for loop. The first one just prints out a list of numbers from 2 to the number designated by the user. In my case that variable is user_input.

Code:
// Alen
// Prime
//

# include <iostream>
using namespace std;

[Code] ....

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved