P: 48

Hi, Im just a kid and I program in C++, and I started this week and know about basic input ouput, control structures, blah blah blah.
I wanted to make a program that keeps counting forever, and pulls out prime numbers, sending them to a specified .txt file.
Could anyone help me?
 
Share this Question
Expert Mod 2.5K+
P: 4,677

Sure, I would start by deciding on how the program will identify a prime number. If you can write that into a function, you can call it every time you hit a number you want. After that, the rest of the program is just an infinite loop that increments a count, and then calls that check.
 
P: 48

thanks sicarie.
could you give me an idea of the prime number method?
I use visual studio express C++ 2005
  Expert Mod 2.5K+
P: 4,677

thanks sicarie.
could you give me an idea of the prime number method?
I use visual studio express C++ 2005
I would imagine that you could brute force it by using the modulus ( % ) operator, and checking all the numbers 29. And, obviously, you don't need to do it on any even number outside of two.
 
P: 48

It makes sense! I'll try and figure it out from there.
If anyone else has any other suggestions or any code to help, please post it.
Thanks sicarie.
one more thing, what function could i use to make a code that finds the power of numbers, so you can let the user enter number and indices like:
NUMBER:
9
INDICES:
2
EXPRESSION FORMED: 9^2
OUTPUT: 81
I know how 2 do all of that apart from the formula 2 get the output.
  Expert 10K+
P: 11,448

It makes sense! I'll try and figure it out from there.
If anyone else has any other suggestions or any code to help, please post it.
Thanks sicarie.
one more thing, what function could i use to make a code that finds the power of numbers, so you can let the user enter number and indices like:
NUMBER:
9
INDICES:
2
EXPRESSION FORMED: 9^2
OUTPUT: 81
I know how 2 do all of that apart from the formula 2 get the output.
That's not 'indices'; that's an exponent. Have a look at the pow() function in the
math library.
kind regards,
Jos
  Expert Mod 2.5K+
P: 4,677

It makes sense! I'll try and figure it out from there.
If anyone else has any other suggestions or any code to help, please post it.
Thanks sicarie.
one more thing, what function could i use to make a code that finds the power of numbers, so you can let the user enter number and indices like:
NUMBER:
9
INDICES:
2
EXPRESSION FORMED: 9^2
OUTPUT: 81
I know how 2 do all of that apart from the formula 2 get the output.
Looks like you just need to read two variables in, and then use the function in the math.h library.
Check out cplusplus.com's tutorials section. They're pretty good.
 
P: 48

thanks guys.
i found the pow function!
  Expert 100+
P: 463

You can cut down on the exhaustive search by only using prime numbers that you've found. You will need to keep a list of them for this to work, but you have them anyway.
You don't have the check the whole range of the number, just check up to the integer ceiling of square root of the number being checked (whew!).
For example, to check that 23 is prime, just check all the prime numbers <= 5. Check (i.e. modulo == 0) for 2, 3, and 5. Stop when you find the first factor. In this case, 23 is prime.
 
P: 48

Could anyone write me up a code?
  Expert Mod 2.5K+
P: 4,677
   Expert Mod 2.5K+
P: 4,677

oh, i see
never mind
We're here to help you, not do your work for you. That means if you get stuck, you can post your code and errors, and we'll help figure it out, but we won't write it for you.
 
P: 48

We're here to help you, not do your work for you. That means if you get stuck, you can post your code and errors, and we'll help figure it out, but we won't write it for you.
Alright, I understand that. I saw the link.
Thanks for your help anyway
    Question stats  viewed: 1774
 replies: 13
 date asked: Aug 13 '07
