473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prime Factors

Hello,
I'm taking an independant course in C++, and one of my questions asks
to use the following algorithm to deturmine the factors of any given
number:
--

Initialize a counter at 2
So Long as long as the counter is less than or equal to the number
if the counter divides the number evenly
display the counter
divide the number by the counter to get a new number
else
add one to the counter

--

I don't know exactly what "divide the number by the counter to get a
new number" is asking.

This is the code I have so far, I know it's incomplete and not close,
but I am completly lost.

/* 4-31 Exercise 12 - A program to display prime factors */

#include <iostream>
using namespace std;

main() {
int Number;

cout << "Enter Starting Number: ";
cin >> Number;

cout << "Prime Factors: "

for(int n=2; n <= Number;) {
if(Number%n == 0){
cout << n;

}
else {
n++;
}
}

}

If you're wondering about school/project cheating, my teacher asked me
to post this, she doesn't know C++.

Thanks alot
-Freyr

Feb 28 '06 #1
7 5213
* Freyr:
I'm taking an independant course in C++, and one of my questions asks
to use the following algorithm to deturmine the factors of any given
number:
--

Initialize a counter at 2
So Long as long as the counter is less than or equal to the number
if the counter divides the number evenly
display the counter
divide the number by the counter to get a new number
else
add one to the counter

--

I don't know exactly what "divide the number by the counter to get a
new number" is asking.
That means:

Replace the number with <the number divided by the counter>.

and the reason it's there is so as to remove this prime factor from the
number so it won't be listed again (if the same value occurs umpteen
times as prime factor, it will be listed exactly umpteen times).

This is the code I have so far, I know it's incomplete and not close,
but I am completly lost.

/* 4-31 Exercise 12 - A program to display prime factors */

#include <iostream>
using namespace std;

main() {


'main' must have result type 'int'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 28 '06 #2
In article <1141149975.533 951.14730
@t39g2000cwt.go oglegroups.com> , lo*****@gmail.c om says...
Hello,
I'm taking an independant course in C++, and one of my questions asks
to use the following algorithm to deturmine the factors of any given
number:
--

Initialize a counter at 2
So Long as long as the counter is less than or equal to the number
if the counter divides the number evenly
display the counter
divide the number by the counter to get a new number
else
add one to the counter

--

I don't know exactly what "divide the number by the counter to get a
new number" is asking.


Ignore programming for the moment, and think about how
you'd do this in your head. Assume, for example, that the
original number is 60. You start by seeing whether that
divides by 2. It does so you print out 2. You divide 60
by 2 to get 30. You check whether 30 divides by 2, and it
does as well. You print out 2 again, then divide 30 by 2
to get 15. You check whether 15 divides by 2, and it
doesn't. You increment your counter to 3 and start over
from the top -- 15 divides by 3, so you print out three,
and then divide 15 by 3 to get 5. 5 doesn't divide by
three, so you increment your counter to 4. 5 doesn't
divide by 4 so you increment your counter to 5. 5 divides
by 5 so you print out 5. You're now done.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 28 '06 #3
Freyr wrote:
I'm taking an independant course in C++, and one of my questions asks
to use the following algorithm to deturmine the factors of any given
number:
--

Initialize a counter at 2
So Long as long as the counter is less than or equal to the number
Actually you can safely stop at counter equal to square root of the
number plus one.
if the counter divides the number evenly
display the counter
divide the number by the counter to get a new number
else
add one to the counter

--

I don't know exactly what "divide the number by the counter to get a
new number" is asking.

This is the code I have so far, I know it's incomplete and not close,
but I am completly lost.

/* 4-31 Exercise 12 - A program to display prime factors */

#include <iostream>
using namespace std;

main() {
int main() {
int Number;
Useful to initialise it to something...

int Number = 42;

cout << "Enter Starting Number: ";
cin >> Number;
Beware that if you don't enter any digits, the conversion will fail and
'Number' will remain at whatever you initialised it with. It may be
useful to check its contents here and notify the user if the value is
invalid.

cout << "Prime Factors: "

for(int n=2; n <= Number;) {
So, 'n' is your "counter". OK.
if(Number%n == 0){
cout << n;

Good so far.

Here you are supposed to "Divide 'Number' by 'n' to get the new 'Number'".
How do you divide? How do you make "new" 'Number' to continue with that
value? Think assignment. Or compound assignment.
}
else {
n++;
}
}

}

If you're wondering about school/project cheating, my teacher asked me
to post this, she doesn't know C++.


To verify that claim it might be useful to have your teacher's e-mail
address...

V
--
Please remove capital As from my address when replying by mail
Feb 28 '06 #4
That's exactly what I needed to know, I just wasn't understanding the
wording, thanks!

So it'd be:

for(int n=2; n <= Number;) {
if(Number%n == 0){
cout << n;
Number = Number/n;
}
else {
n++;
}

or am I missing something else? (I don't have a compiler here, I'll
have to test it tomorrow)

Victor - Knock yourself out: nb*****@staff.e dnet.ns.ca

Alf - Yeah, thanks for pointing that out, I usually check my syntax
after I get the general layout done, but I missed that (How stupid of
me, eh?)

Feb 28 '06 #5
Freyr wrote:
That's exactly what I needed to know, I just wasn't understanding the
wording, thanks!

So it'd be:

for(int n=2; n <= Number;) {
if(Number%n == 0){
cout << n;
Number = Number/n;
A "compound assignment operator" can be used. Look it up.
}
else {
n++;
}

or am I missing something else? (I don't have a compiler here, I'll
have to test it tomorrow)


The only thing I'd add would be some kind of separator between your
outputs, like

cout << n << ' ';

otherwise your numbers are going to be printed frozen together.

V
--
Please remove capital As from my address when replying by mail
Feb 28 '06 #6
True enough.

I'll give compound assignment operators a look into, I'm learning all
this on my own, so it's not like I'm going out of the confined
operations to do something.

Thanks alot.
-Freyr

Feb 28 '06 #7
In article <1141161602.183 121.294530
@j33g2000cwa.go oglegroups.com> , lo*****@gmail.c om says...
That's exactly what I needed to know, I just wasn't understanding the
wording, thanks!
You're certainly welcome.
So it'd be:

for(int n=2; n <= Number;) {
if(Number%n == 0){
cout << n;
Number = Number/n;
}
else {
n++;
}


This looks fairly reasonable. Victor's already pointed
out the use of a compound assignment, so I won't belabor
that point.

You might also want to consider using div() to compute
the quotient and remainder/modulus together. It's not
guaranteed to improve anything, but it might. Usually if
you compute one, you get the other thrown in for free,
and div allows you to use both from a single computation
instead of doing the computation twice. Then again, a
good optimizer may already take care of that...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
8410
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
2717
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...
5
2727
by: Rahul | last post by:
HI. Python , with its support of arbit precision integers, can be great for number theory. So i tried writing a program for testing whether a number is prime or not. But then found my function painfully slow.Here is the function : from math import sqrt def isPrime(x): if x%2==0 or x%3==0: return 0
11
5946
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.
10
4443
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone has time to critic and offer my some feedback I'd be grateful Thanks Joel
7
758
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
3
5088
by: rcarwise | last post by:
Iam having trouble getting started on this program and wanted to know if I could get help on writing a method and loops to get started. this is the program that I have to do: Write a program that reads an integer number n > 1, and writes the input number’s prime factors. The following are a few input/output examples: Input Output 12 2,2,3
12
6229
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I have so far. Problem 1: Is it a prime number? Write a Python program that allows the user to enter a whole number greater than 1 and that determines whether or not this number is a prime number. If it is a prime number, then this information is...
0
9687
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
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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
10257
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
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.