473,890 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prime factoring progam giving me problems

6 New Member
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 1594
sicarie
4,677 Recognized Expert Moderator Specialist
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 New Member
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 New Member
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 New Member
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

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

Similar topics

36
8424
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, but a prime number tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range so it would have to fairly efficient Dag
9
2718
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 (testPrimes.py) with two algorithms that both check for primes by testing only odd numbers using factors up to the square root of the value, where Primes1 is based on all of the existing primes so far, and Primes2 is based on all odd numbers, I would...
11
5958
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 (inclusivly) evenly divides n.
4
11512
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 integers are prime numbers. I have been unsuccessful in finding anything. I am sure I can probably figure out a long way to do it, but I was curious to see if C++ had
20
6836
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 class, and am working on building a RSA public key cryptology system for a class project. I am building the librarys just to get the experience to do so. However, I would ask if any of you know of any gaping security holes that can easily be seen from...
25
3908
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 complicated. we are on a functions chapter. i am usung the square root to find the prime. so far i have accomplished this but i cant fgure how to divide this by ten so i can find the right prime. i am using c. #include <stdio.h>
0
1104
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
1376
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 compiled with Visual Studio 2005 - I hope you had no problems with VC++ 6.0?
6
5300
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 number of digits i can use? For example, when i use the number 4362206209 (which I know for a fact is prime) it returns 2 as a factor, which it isn't. Eventually, I would like to get to long (50+ digit) numbers. I know this calculation would take...
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10810
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9625
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4674
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3275
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.