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
ADVERTISEMENT
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
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
Jan 9, 2013
#include<iostream>
#include <cstdlib>
using namespace std;
[Code].....
View 10 Replies
View Related
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
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
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
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
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
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
Nov 25, 2012
I need to write a function with an array parameter and an int parameter.
That array has to be filled with first 10 prime numbers that are exact or higher than the int parameter... and then i need an average value of those 10 prime numbers...
The problem is im not really sure how i should do the part to fill the array with prime numbers that are higher than that int?? So any code samples that does that would be really nice... (the average part shouldnt do much problems i think, so i only need for that array fill...)
Code:
int avgprimearray (int higharray[], int somenumber){
}
View 4 Replies
View Related
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
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
Dec 7, 2013
Question: How to find a duplicate numbers and numbers found once in array.
View 7 Replies
View Related
Aug 25, 2013
I wrote this GCD function to find gcd of an array of numbers . Here is the code:
long long GCD(long long min,int loc,long long b[]) {
for (long long i=min;i>0;i--) {
bool p=0;
for (long long x=0;x<loc;x++) {
if (b[x]%i!=0)
[Code] ....
Its returning wrong answers. When i tried 6 14 it returns 6
View 6 Replies
View Related
Jul 17, 2012
When I run my program it just gives me 'The smallest number is: 2682760' which isn't true obviously.
Code:
#include <iostream>
int main() {
using namespace std;
int x, smallest;
int Array[7] = {7, 5, 10, 13, 3, 15, 54};
x = Array[0];
[Code] .....
View 3 Replies
View Related
Jun 20, 2012
find the average of 10 numbers to an array using function .the array should be controlled by while loop?
#include <iostream.h>
float sum(float x[],int size);
main() {
float a[10];
int n=10;
[Code] .....
View 2 Replies
View Related
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
Oct 24, 2014
I need to find the smallest number in my 10x8 arraygrid with random numebr filled in it
Here my Code:
#include <iostream>
using namespace std;
int main() {
int total,average,smallest;
int row=0;
int col=0;
[Code] ....
View 2 Replies
View Related
Jun 6, 2014
I have a question regarding finding similar 4 digit numbers in a pool of 120 numbers
Below is an linear single dimension array of 4 digits
There are 120, 4 digit numbers in total
My question is this - How can I code in C - a function that looks through all the 120, 4 digits to find similar numbers
Example - 2095 is similar or matches to - 0950, 5095, 5250, 5269 - i.e having 3 of the digits that are the same in the 4
The code must print out 2095 + all the matched numbers
If the Matched or Similar numbers are less then a certain number n - that number is discarded and the code should go onto the next number
2095095053741884864037233464846523768035
4340520865405306753553226100610753235081
1160346508409698809176715645765520676974
2398509523307591808464215318649140982136
2388015030217584311064844010520796345135
5376565155806436092330366745536969232311
4351519149310340620918615194324744655250
5330634052450976531053882380318069765623
2186677440212394367468519636617536556706
5274239549814534091052060118499521655275
6351091153944834003545212360098053955218
4835406061305276769161885611376776845269
I have written some code below but it is not working ...
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
FILE *fileptr;
FILE *fileptr1;
[Code] ....
View 10 Replies
View Related
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
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
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
May 13, 2014
im trying to write a source code that find the smallest, largest and average of numbers in array. the code runs fine, but it is not giving the highest number and the the average should include only four number excluding highest and smallest number from the array.
void OlympicJudging() // Olympic Judging {
int numbers [6];
double average, sum = 0;
int temp;
for(int i = 0; i < 6; i++){
cout << "Please type a value for scores: ";
cin >> numbers[i];
[Code]...
View 5 Replies
View Related
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
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