By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

counting program

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?
Aug 13 '07 #1
Share this Question
Share on Google+
13 Replies


sicarie
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.
Aug 13 '07 #2

P: 48
thanks sicarie.
could you give me an idea of the prime number method?
I use visual studio express C++ 2005
Aug 13 '07 #3

sicarie
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 2-9. And, obviously, you don't need to do it on any even number outside of two.
Aug 13 '07 #4

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.
Aug 13 '07 #5

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
Aug 13 '07 #6

sicarie
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.
Aug 13 '07 #7

P: 48
thanks guys.
i found the pow function!
Aug 13 '07 #8

RRick
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.
Aug 14 '07 #9

P: 48
Could anyone write me up a code?
Aug 14 '07 #10

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

http://www.thescripts.com/forum/faq....ing_guidelines
Aug 14 '07 #11

P: 48
oh, i see
never mind
Aug 14 '07 #12

sicarie
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.
Aug 14 '07 #13

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
Aug 14 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.