Prime numbers... again.

Yea. I suck.

Page 1 of 1

14 Replies - 1916 Views - Last Post: 02 April 2008 - 12:06 PM Rate Topic: -----

#1 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Prime numbers... again.

Post icon  Posted 25 March 2008 - 12:45 PM

I have tried searching for previous posts about finding prime numbers, but I can't really seem to find any that can help me figure out how to impliment them into my code. So I thought I would post what I need help with, and what I have so far. Here it goes. This is only one step to my project, I hope to figure this out so I can do the other half on my own.

Write a program so that the user will enter two integers say x and y. As long as x>y, the program will print all primes between x and y inclusive. Also, write a function named findPrime which accepts two integer parameters say a and b. This function will print all prime numbers from a to b by calling the function isPrime. Have your program call this function busing the user input x and y.

My code:

// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>



using namespace std;



int main()
{	int x;
	int y;

	printf ( "Please enter two positive integers-->");
		scanf ( "%d" "%d" , &x, &y );

		printf ( "\nPRIME: ");
		

		
		

}

int findPrime ( int a, int b)




If 3 and 30 are entered for x and y, the program should produce the following output:

Prime: 3 5 7 11 13 17 19 23 29
(Prime count =9)


I'm not really sure where to start, not sure how to call isPrime to check to see if its prime or not, pretty sure I know how to write a code to figure out the prime number for 1 number, but not to list in between, just really confused, I know I need a for loop somewhere, and of course a while loop, I could really use help on how the program should look, (an algorithm would be nice). Well, any tips, pointers, or anything else would be greatly appreciated.

Adot2the~

Is This A Good Question/Topic? 0
  • +

Replies To: Prime numbers... again.

#2 steelersfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 08

Re: Prime numbers... again.

Posted 25 March 2008 - 01:13 PM

try out my previous posts and responces to them. i have a very similar program that calculated up to the first 100,000 primes and then printed out a section of primes such as your x-y..let me know if it helps..if not i can try to give you some clips of my program to help you

<http://www.dreamincode.net/forums/index.php?showtopic=45246&hl=prime>
Was This Post Helpful? 0
  • +
  • -

#3 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 25 March 2008 - 01:23 PM

still lost, not sure how to write my functions, or how my program should look :(

thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#4 steelersfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 08

Re: Prime numbers... again.

Posted 25 March 2008 - 02:25 PM

haha well then what functions do you need to have and such?

algorithm for your program would be kind of like:
ask user for x and y inputs
calculate prime numbers between x and y using a for loop
print prime numbers calculated

i know it seems vague but that could maybe help..is there a specific number the user is suppose to not go over or anything??
Was This Post Helpful? 0
  • +
  • -

#5 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 25 March 2008 - 05:16 PM

View Poststeelersfan, on 25 Mar, 2008 - 02:25 PM, said:

haha well then what functions do you need to have and such?

algorithm for your program would be kind of like:
ask user for x and y inputs
calculate prime numbers between x and y using a for loop
print prime numbers calculated

i know it seems vague but that could maybe help..is there a specific number the user is suppose to not go over or anything??


well if the user enters 3 and 30, it should calculate all of the prime numbers between the two, and not go over 30 if thats what you mean. But no, it should be able to calculate the primes between any range of numbers, the only problem that I have is writing functions to do so, I don't know what an IsPrime function should look like, and a FindPrime function should look like. I don't know how to call the functions to print the numbers out.
Was This Post Helpful? 0
  • +
  • -

#6 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 26 March 2008 - 03:15 AM

can anyone show me how it would look?
Was This Post Helpful? 0
  • +
  • -

#7 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Prime numbers... again.

Posted 26 March 2008 - 03:58 AM

Let's say we have a function int prime(int a, int B)
int prime(int x,int y)
{
int i,j;
bool x=true;
if (y>x){
for (i=a;i<b;i++)
{   for (j=2;j<i;j++)
         if (i%j=0)
            x=false;
     if (x==true)
         return x;
}
return -1;
}
else
return -1;
}
//this means if no prime is found return -1.
return -1;
}


now my idea for the main was something like this:
int main()
{
int x,y,i,prime;
scanf("%d%d",&x,&y);
prime=x;
do {
prime=prime(prime,y);
printf("%d",prime);
}while(prime!=-1);
return 0;
}


I hope i didn't complicate it to something unworking, but the algorithem seems right.
Was This Post Helpful? 0
  • +
  • -

#8 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 27 March 2008 - 01:57 AM

hmm not sure why im getting errors.

// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>


using namespace std;

int isprime( int a, int b)
{	int i;
	int p;

	for (i=2;a<b;i++) 

		if (a%i!=0) {
			p++;
			return 0; 
		else
			return i
						}
}

int main()
{	int x;
	 int y;

	printf ( "Please enter two positive integers-->");
		scanf ( "%d" "%d" , &x, &y );

		printf ( "\nPRIME: ");

		isprime(i);

   return 0;
}



Just need the program to call isprime to keep printing the value of i. The variable i should be the prime numbers between x and y.

confused lol.
Was This Post Helpful? 0
  • +
  • -

#9 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 28 March 2008 - 01:09 AM

bump, could use help.. lol.
Was This Post Helpful? 0
  • +
  • -

#10 steelersfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 08

Re: Prime numbers... again.

Posted 28 March 2008 - 04:36 AM

what is the error message you are getting back??
Was This Post Helpful? 0
  • +
  • -

#11 steelersfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 08

Re: Prime numbers... again.

Posted 28 March 2008 - 04:41 AM

should you maybe also call the isprime function before you call your print function? maybe im wrong though

and also your calculations aren't going to be correct you need another nested for loop...like Sepanto showed you above in his first bit of code he posted
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Prime numbers... again.

Posted 28 March 2008 - 07:24 AM

You are supplying a variable named i to the function isprime(). you have not declared a variable named i, nor have you instantiated it.
Was This Post Helpful? 0
  • +
  • -

#13 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 31 March 2008 - 12:43 PM

so uh.. how do i get those numbers to show on the side of the document? lol it would really help me fix my errors :)
Was This Post Helpful? 0
  • +
  • -

#14 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 31 March 2008 - 01:08 PM

// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

#define TRUE 1
#define FALSE 0









void main(void)
{	
	int x;
	int y;
	
	 
	

	printf ( "Please enter two positive integers-->");
	 scanf ( "%d %d" , &x, &y );

		printf ( "\nPRIME: ");
		while ( scanf( "%d %d" , &x,&y)==2 )
		{ 
			findprime(x,y);
			printf ( "Enter two positive integers-->" );
			scanf ( "%d %d", &x, &y );
		}


   

}

void findprime( int a, int b) /* finds the prime number*/
{							/* checks to see if the numbers between x and y are prime */
	int p; //number of primes
	int i; //divisor
	isprime (int)

	
	{

		for (i=2; a<b; i++)
		{	if (isprime(i)) 
			
				printf ( "\t%d" , i)
			
		}
	}

		
			
}

	



int isprime(int k)
{	
	
	if ((a%i!==0)) // makes it prime?
			p++;
			return TRUE;
	else
		return FALSE;
}



my code, but still tons of errors.

Just really want to get a print out from my code for once, even if its wrong, just to see numbers print out. It would be so much easier to fix it.
Was This Post Helpful? 0
  • +
  • -

#15 Adot2the  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 07-February 08

Re: Prime numbers... again.

Posted 02 April 2008 - 12:06 PM

Ok so here is my working code that almost works!



// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

#define TRUE 1
#define FALSE 0


void main(void)
{   void findprime(int, int);
	int x;
	int y;
	
	 
	

	printf ( "Please enter two positive integers-->");
//	 scanf ( "%d %d" , &x, &y );
	 while (scanf("%d %d", &x, &y)==2 )
	 {
		 findprime ( x, y );
		 printf ( "Please enter two more positive integers-->" );
//		 scanf ( "%d %d" , &x, &y);
	 }
		
		


   

}

void findprime( int a, int b) /* finds the prime number*/
{							/* checks to see if the numbers between x and y are prime */
	int p; //number of primes
	int i; //divisor
	int c=0; // counter of number?
	int isprime(int);

	

		for (i=2; i<b; i++)
		{			
					if (isprime(i)) 
					{
						printf ( "\t%d" , i);
						c++;
					}
				
					
				if (c==b)
				{ 
					putchar ('\n');
					c=0; 
				}
			
			
		
	

		
			
		}

	
}


int isprime(int k) //will consist of the formula for finding a prime number
{	int i;
	
	for (i=2;i<k;i++) 
	
	

	
	if ((k%i!=0)) // makes it prime?
			return TRUE;
	else
		return FALSE;
}



So... how do I get it to print the prime numbers? Is there something wrong with my for loop?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1