C/C++ :: Speeding Up A Prime Function?

May 17, 2014

I'm doing a problem on spoj and i'm sure that the function on using to find prime numbers is the reason why my program is to slow for spoj. I need the function to be able to print number within the given range which is why i'm using one array to hold the min and one to hold the max from user input. I'm having trouble thinking of a way to make the function faster without breaking the function.

#include <cstdio>
inline void primeFunction(int min[], int max[], int testCase) {
int maxRange = 0, count, num, j, minRange = 0;

[Code].....

View 2 Replies


ADVERTISEMENT

C++ :: Speeding Up VGA Emulation?

Jan 17, 2014

Any tips regarding speeding up VGA emulation? (I'm currently processing the pixels, one at the time, from VGA VRAM to the emulated screen. The rendering happens after every frame) Currently at avg. of 50 microseconds per pixel (text mode), 50 milliseconds per line (int10 mode 04h).

View 5 Replies View Related

C :: Pointers For A Prime Function?

Oct 15, 2013

i'm trying to determine if a number from a file is a prime number using pointers first I wrote a function that determines if the number is a multiple of 7, 11, or 13. Then i wrote a function to see if the number is odd or even. Are they correct? Later i will print the results on screen but i'm extremely confused with pointers and i'm not sure how to write this prime function...

Code:

void divisible(int *n, int *result) {
if (*n % 7 == 0 || n % 13 == 0 || n % 13 == 0) {
*result = 1;

[Code] .....

View 2 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++ :: 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++ :: How To Set Up Prime Function Program Within Menu

Oct 16, 2013

Can not seem to get the program to perform the calculations within the cases. Also, how do you set up the a prime function program within the menu. Have been trying for five days now.

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void showChoices();
double prime(double, double);
double abs(double, double);
double pow(double, double);

[Code]...

View 1 Replies View Related

C++ :: Function To Determine And Print Prime Numbers

Mar 30, 2013

/* Task: create a function that determines prime number and use it to print out prime numbers from 0-50: */

Function prototype: Should return a `bool` (because it answers a yes/no question) and should take in a single int (to ask whether it's prime).

- An int greater than 2 is prime if it is not divisible by any number between 2 and itself, not including itself.
- Use this function to print out the primes less than 50. Check it against my list:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

#include <iostream>
using namespace std;
int prime(int&x) {
if(x%2==1)

[Code] ....

It is printing out the correct prime numbers but also printing this between each number: 1629974960

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 :: Prime Number Function - Declaration Syntax Error

Jan 8, 2014

I wrote this program and compiled in turboc, but it gets error"declaration syntax error" .

Code:
#include <stdio.h>
int main()
int isprime(int n)
{
if(n<2)
return 0;

[Code] ....

View 5 Replies View Related

C :: Recursive Function For Finding And Summing Prime Factors?

Mar 6, 2015

main function:

Code:
int main(){
int n, s = 0;
printf("Insert number: "); scanf("%d", &n);
if (n == 0 || n==-1 || n==1){ printf("No prime factors!
"); return 0; }
if (n < -1) { n = - n; printf("Prime factors: -"); }

[Code] ....

Recursive function

Code:
static int i = 2;
int primefactors (int n) {
if (n == 1) return 0;
if (n%i == 0) {
printf("%d ", i);
return i + primefactors(n / i);

[Code] ....

View 8 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 :: 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++ :: Number Is Prime Or Not

Nov 7, 2013

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

[code]....

its supposed to tell whether the inputted number is prime or not

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 Factors Of A Number?

Oct 22, 2013

I have to create a program where I need to find the prime factors of a number and express it using exponents.

For example, for the number 1368 should output:

1368 = 2^3 * 3^2 * 19^1

This is my code:

void primeFact (int n){
int i;
int score = 0;

[Code].....

View 3 Replies View Related

C++ :: Printing Out Prime From Array

Nov 4, 2013

I am trying to print out prime from array. I have viewed other sources that are similar and tried to use as reference, but only to confuse myself. This is what i have so far.

void get_output(int&Max,int array[]) {
//array[i]-i;
for(i=1;i<=10;i++) // allow 10 outputs on a line. {
for(int i=0;i<Max;i++) {
prime=true;

[Code] ......

View 4 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++ :: Prime Number Factorization

Dec 9, 2013

I am trying to factorize semi-prime numbers by using a brute force method and a square root method.The brute force method should start by multiplying all odd numbers between 1 and the semi-prime number. Meaning that 1 should multiply 3 then 5 then 7, etc. Then three should multiply 5 then 7 then 9 and so forth. The first number would never multiply a number equal to or less than itself.

bruteForce(int x){
f1 = 1;
f2 = f1 + 2;
while (f1 < x){
for (f2 = x; f2 <= x; f2-2){

[Code] ....

For my other algorithm, I want it to get the square root of the semi-prime and then start multiplying odd numbers around it. I made it so that the square root would have to be set to the next even number (in case it was a factor). If the product of the two odd numbers (a * b) is greater than the original semi-prime, I would multiply b by the next smaller number than a. Conversely, if the product of a and b is less than the original semi-prime, it will multiply a by the next odd number larger than b. This will repeat until the two correct factors of the original semi-prime are found.

Algorithm:

SquareRootFactorize(int x){
int N = x;
sqrtN = sqrt(N);
if (isOdd(sqrtN) == true)
sqrtN = sqrtN + 1;
f1 = sqrtN - 1;
f2 = sqrtN + 1;

[code].....

View 1 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++ :: Prime Number Between Two Values

Mar 14, 2014

I wrote this program to print prime numbers between 2 and 10 but it only prints '3'. here is the code

#include<iostream>
#include<conio.h>
using namespace std;
int prime(int x, int y);
void main(){
int n=2;

[code].....

View 6 Replies View Related

C/C++ :: Prime Factorization Program?

Mar 12, 2014

the only thing is I want multiple all prime number I got. such as when I enter 12 the output should be "the prime factorization of 12 is 2*2*3" right now the program output is "2 2 3"

# include <stdio.h>
# include <math.h>
void primeFactors(int n)
{

[Code]....

View 8 Replies View Related

C/C++ :: MPI Prime Number Search?

Mar 14, 2015

We are working on building a cluster and needed to create or find a program writen in C that we can run. I was thinking a program that finds prime numbers and insert them into a MySQL database. I have never worked with C so I don't know where to start or anything. I have a week to prove our project is worth keeping and our cluster is almost completely installed we just need to create a program.

View 4 Replies View Related

C++ :: Finding If A Number Is Prime Or Not

Oct 3, 2013

cin.ignore (10);
return 0;
}

Its not working

View 2 Replies View Related

C/C++ :: Prime Number Counter?

Feb 23, 2014

I have to write a program that asks the user to input a positive integer, counts how many primes there are before this integer, then divide the number of primes by the integer. Here's what I have so far:

int n;
int counter = 0;
cout<<"Enter a positive integer n.";
cin>>n;

[Code]....

the value of the integer counter is supposed to increase whenever a prime number is identified but it's increasing n times

View 8 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







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