473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

prime factoring progam giving me problems

6
I need to print out the prime numbers of an integer entered in. I am new to C++ but wrote this program with my partner and while it compiles, that's about all it does. I'm pretty sure it gets lost in the for loop, or atleast that's where I get lost.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.  
  6. {
  7. int valueToFactor;
  8.  
  9. cout << "Enter Number to Factor: "<< endl;
  10. cin >> valueToFactor;
  11.  
  12.  
  13.  
  14. for(int n=2; n <= valueToFactor;){
  15.     if(valueToFactor%n == 0){
  16.  
  17.         cout << "The Prime Factors are:" << n << endl;
  18.         valueToFactor = valueToFactor/n;
  19.     }
  20.      else 
  21.  
  22.     ++n;
  23.     }
  24.  
  25. return(0);
  26. }
  27.  

I was wondering if anyone had any hints to get me moving again.
Thanks.
RyeGuy
Mar 20 '07 #1
12 1565
sicarie
4,677 Expert Mod 4TB
I need to print out the prime numbers of an integer entered in. I am new to C++ but wrote this program with my partner and while it compiles, that's about all it does. I'm pretty sure it gets lost in the for loop, or atleast that's where I get lost.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.  
  6. {
  7. int valueToFactor;
  8.  
  9. cout << "Enter Number to Factor: "<< endl;
  10. cin >> valueToFactor;
  11.  
  12.  
  13.  
  14. for(int n=2; n <= valueToFactor;){
  15.     if(valueToFactor%n == 0){
  16.         cout << "The Prime Factors are:" << n << endl;
  17.         valueToFactor = valueToFactor/n;
  18.     }
  19.      else 
  20.     ++n;
  21.     }
  22. return(0);
  23. }
  24.  
I was wondering if anyone had any hints to get me moving again.
Thanks.
RyeGuy
You only increment n in the 'else' statement, which means that if you enter the 'if' portion, n stays the same through the loop (as soon as it's even, it gets stuck). You could just increment n no matter what, and print out each factor individually.
Mar 20 '07 #2
Roonie
99
well, i have two things to mention.

first off, it doesnt look like youre set up to count in primes. it looks like youre trying to use the iterator for your for loop to factor the input number when you need to use a different variable. (youre counting by 2,3,4,5,6, . . . and you need to be factoring by 2,3,5,7,11, . . . )

i think you also need some sort of data structure to hold the factors youve already found so far too. you need to keep track of how many times you can divide by two, and then three, and five . . . etc.
Mar 20 '07 #3
Ryeguy
6
You only increment n in the 'else' statement, which means that if you enter the 'if' portion, n stays the same through the loop (as soon as it's even, it gets stuck). You could just increment n no matter what, and print out each factor individually.


I was thinking of this, you mean increment it in the for loop? I'm not sure how i would get each factor to print out then.
Mar 20 '07 #4
sicarie
4,677 Expert Mod 4TB
I was thinking of this, you mean increment it in the for loop? I'm not sure how i would get each factor to print out then.
I would recommend a for loop - then you can specify an action for each separate iteration of that loop.

Also look at Roonie's post - I think you're missing something with your algorithm, you're only counting the divisors, not the primes (though I think that would be the first step, if you stored those, then you could find the ones that were primes with an extra iteration),
Mar 20 '07 #5
Ryeguy
6
well, i have two things to mention.

first off, it doesnt look like youre set up to count in primes. it looks like youre trying to use the iterator for your for loop to factor the input number when you need to use a different variable. (youre counting by 2,3,4,5,6, . . . and you need to be factoring by 2,3,5,7,11, . . . )

i think you also need some sort of data structure to hold the factors youve already found so far too. you need to keep track of how many times you can divide by two, and then three, and five . . . etc.
Thanks For the quick responses!

Roonie would it be easier for me to have n start at 1 and then increments by 2? Also I am not sure how I would add something to hold the prime factors, sorry im not understanding very much, this is all new to me.
thanks again.
ryeguy
Mar 20 '07 #6
sicarie
4,677 Expert Mod 4TB
Thanks For the quick responses!

Roonie would it be easier for me to have n start at 1 and then increments by 2? Also I am not sure how I would add something to hold the prime factors, sorry im not understanding very much, this is all new to me.
thanks again.
ryeguy
An integer array would be able to store the numbers - it depends on what else you want to do with them, but I'm pretty sure an array would be easiest.
Mar 20 '07 #7
Roonie
99
yeah, i would say that an int[] where each successive member represented how many times each of the primes factored would do nicely to store that information.

and as for counting in primes, that is something that professional computer programmers try to solve in their work. is there any upper limit to your input? if so, it might be easiest to simply hard-code the primes up to that limit. if not, im sure i can scratch up a reference or two to some prime-number finders . . .
Mar 20 '07 #8
Ryeguy
6
An integer array would be able to store the numbers - it depends on what else you want to do with them, but I'm pretty sure an array would be easiest.
I just recently learned about arrays but wouldn't i need to know how many prime integers there were to start the array?
Mar 20 '07 #9
Ryeguy
6
yeah, i would say that an int[] where each successive member represented how many times each of the primes factored would do nicely to store that information.

and as for counting in primes, that is something that professional computer programmers try to solve in their work. is there any upper limit to your input? if so, it might be easiest to simply hard-code the primes up to that limit. if not, im sure i can scratch up a reference or two to some prime-number finders . . .

I'm not sure if there was meant to be an upper limit but there was none given.
Mar 20 '07 #10
sicarie
4,677 Expert Mod 4TB
I just recently learned about arrays but wouldn't i need to know how many prime integers there were to start the array?
You can always grow the array dynamically...it would be annoying, but you could do it.

You could also use a vector, or even a list, which are designed to be able to be added to on the fly, but requires contiguous free memory space.
Mar 20 '07 #11
Roonie
99
yeah, i dont think there is any better way to dynamically grow a simple array than to simply copy the small array to a new, bigger array. if youre unsure how much data you have to cope with, i would definitely suggest using a more advanced data type.

as for a good way to find prime numbers, im looking. i think how you do it really depends on how much processing time you can devote to finding a prime number. if this is something that "just has to work" you can write an algorithm to brute-force it. what sort of a program are you writing?

i dunno, im on google right now . . . i seem to remember something about a guy named fermat in conjunction with primes? . . . its been a while since ive needed prime numbers too.
Mar 20 '07 #12
Ryeguy
6
yeah, i dont think there is any better way to dynamically grow a simple array than to simply copy the small array to a new, bigger array. if youre unsure how much data you have to cope with, i would definitely suggest using a more advanced data type.

as for a good way to find prime numbers, im looking. i think how you do it really depends on how much processing time you can devote to finding a prime number. if this is something that "just has to work" you can write an algorithm to brute-force it. what sort of a program are you writing?

i dunno, im on google right now . . . i seem to remember something about a guy named fermat in conjunction with primes? . . . its been a while since ive needed prime numbers too.

Hey thanks for your help, i figured a program out late last night!

thanks
ryan
Mar 20 '07 #13

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

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
4
by: Chris | last post by:
Hello! I have been doing some research to see if C++ has some sort of function to work with prime numbers. For example, I would like to input integers and have the program check to see if the...
20
by: Tuvas | last post by:
I have made and recently posted a libary I made to do Modular Arithmetic and Prime numbers on my website at http://www.geocities.com/brp13/Python/index.html . I am currently in a crypotology...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
0
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
by: fabien.lyon | last post by:
-----Message d'origine----- De : fabien.lyon Envoyé : mercredi 30 mai 2007 20:16 À : 'python-list@python.org' Objet : RE: embeded python progam into visual C++ application crash 2.5.1 is...
6
by: UofFprogrammer | last post by:
For a fun challenge, I am trying to create a C++ program that will determine if a number is prime. I am getting to problems when I reach a large values (over 9 or so digits). How can I increase the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.