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


ADVERTISEMENT

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 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++ :: 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 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++ :: 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 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++ :: 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++ :: Random Numbers Are Not In Specified Range

Nov 9, 2014

My program behaves weird... I wanted to generate 10 random numbers from 1 to 100 each of them bigger than previous, using the while loop and function that returns a random number in specified range.

When I run the program, I get numbers much bigger than 100, even negative number, and numbers are same every time I run the program.

Code:
#include <ctime>#include <cstdlib>
#include <iostream>
using namespace std;
int range(int low, int high);

[Code] .....

View 2 Replies View Related

C++ :: Finding A Range Between 2 Numbers?

Sep 23, 2013

Ok so I'm reading the Programming: Principles and Practice using C++ and Im stuck in Drill 4 part 5. It says:

Change the program so that it writes out the "numbers are almost equal" after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/10000000

I'm using an If statement for it... I just need know what the formula is to check 2 numbers that were entered by person if they land within the range specified above. so then I can cout << "numbers are almost equal" << endl;

View 4 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++ :: How To Make Array With A Range Of Numbers From 1 - 9 Only

Jan 29, 2013

I want to make an array with a range of number from 1-9 only. So if the user entered more than or less than the number indicated it will be error and ask the user to reinput the data. So far my code can be used to do so that if the user enter the number one by one... But if the user entered all the number in one shot the reentered value will be prompt to the back automatically for some reason...Let say 10,2,3,4 which was suppose to be 1,2,3,4 became 2,3,4,1 instead... here is the code:

#include<stdio.h>
void main() {
int num[4][4];
int row,col,x,y;
for(row=0;row<4;row++)

[Code] .....

View 3 Replies View Related

C/C++ :: Finding Happy Numbers For A Given Range

Oct 5, 2014

I'm currently working on an assignment that finds all happy numbers for a given range. Such as 1- 10. My current code just isn't cutting it. I have to use a user defined function and return a bool value to main in order to output if the number is happy or if it sad. For some reason, I cannot get it to loop properly. the first number will be displayed correctly, but the second and third are just squares of the first number, instead of being squared by their single digits. I'm dividing by 10 to get the first number of the digit entered, then I'm using mod to get the remainder. One thing I did notice was that the num2 was putting a remainder of 30 for 130, and I'm not too sure what I need to do to change that, without affecting the whole program.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
Program calculates the bill for a user

[Code] ....

View 8 Replies View Related

C/C++ :: Get Double Random Numbers In Range From 0 To 1?

Apr 19, 2012

How to get double random numbers in the range from 0 to 1?

View 2 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++ :: 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







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