![]() |
|
#1
|
|||
|
|||
Prime Factorization ProblemsSo my assignment is to type a number and have the program tell the prime factors, but when i type a number in it outputs numbers that are not prime numbers. for example when i put 30 in the program outputs 1,2,3,5,6,10, and 15 are prime factors of 30. i know this is not true and i can not figure out what needs to be changed so that the output is correct.
CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: Prime Factorization ProblemsYou are printing out anything that divides the number entered:
CPP / C++ / C Code:
You're not checking if i is prime, so you can't expect only prime numbers to be printed. While you could solve this problem by writing a function to test primality, it would be better to use a recursive approach and avoid the primality test altogether. You could start like this: CPP / C++ / C Code:
I've simply created a function for printing prime factors, and pasted your code inside it. As is, it will do exactly the same thing. However, once you code is in this form it is easy to change it into a recursive algorithm. The trick is that when you find an i such that number%i == 0, you can recursively call printPrimeFactors on i and number/i instead of printing i, and then return. If you reach the end of the loop without finding an i such that number%i == 0, then number must by prime, so you should print out number. Also make sure that your loop starts from 2, not 1. __________________
www.blake-foster.com |
|
#3
|
|||
|
|||
Re: Prime Factorization ProblemsQuote:
A integer greater than 2 is a prime number if it has no factors other than 1 and the number itself. CPP / C++ / C Code:
A few runs, testing numbers from 2 through 30, might look something like Code:
Regards, Dave Last edited by davekw7x : 21-Mar-2010 at 13:20.
|
|
#4
|
|||
|
|||
Re: Prime Factorization Problemsi understand what you are saying but i do not know how to do it. thank you i am going to ask my teacher for help today
|
|
#5
|
|||
|
|||
Re: Prime Factorization ProblemsCPP / C++ / C Code:
This is what i came up with. Thank you for the help |
|
#6
|
||||
|
||||
Re: Prime Factorization ProblemsI didn't want to give away the answer before since this is a school assignment, but since you have solved it now, this is how you would do it recursively (since it never hurts to consider different ways of solving a problem):
CPP / C++ / C Code:
__________________
www.blake-foster.com |
|
#7
|
|||
|
|||
Re: Prime Factorization Problemsi like how that works. the code seems to be very simple it just was i didnt learn what printprimefactors was.
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Could you explain to me what I have to program about this? | disk97 | C++ Forum | 4 | 27-Mar-2009 08:34 |
| Will pay through Paypal if somebody helps me. | paritoshcool | Assembly Language | 0 | 27-Nov-2007 22:27 |
| Python script: Prime numbers | crystalattice | Python Forum | 4 | 16-Apr-2006 08:19 |
| Chaintech Geforce 5600 FX problems | bartster74 | Computer Hardware Forum | 8 | 04-May-2004 13:16 |
| Help w/ prime # determination | crystalattice | C Programming Language | 17 | 18-Apr-2004 21:43 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The