Calling all C++ programers!(not just HL) Created 20 years ago2004-07-30 20:30:53 UTC by MADRAGE MADRAGE

Created 20 years ago2004-07-30 20:30:53 UTC by MADRAGE MADRAGE

Posted 20 years ago2004-07-30 20:30:53 UTC Post #46899
If your an experienced visual C++ programmer, maybe you can help me with this thupid exercise my prof gave me:

Make a program that lets you input one number and outputs all its possible factorials.

example: I input 20, output would be (20,1);(10,2);(5,4);.........

My prof only taught us "IF and ELSE" and "DO and WHILE" statements!
She also gave us a hint about a WHILE inside another WHILE.

Thanks alot in advance for your help guys! ...This isht is really confusing! :zonked: :zonked:
Posted 20 years ago2004-07-30 20:51:34 UTC Post #46901
I would help, but I haven't slept in about 2 days and havent eaten all day. I might help another day.
m0p m0pIllogical.
Posted 20 years ago2004-07-30 21:22:14 UTC Post #46906
Search here

http://www.nist.gov/dads/

I just started programming C++ today, and know the if then do while stuff. I also know the for else, all that. I would consider getting a step ahead of your professor at the tutorial on www.cplusplus.com
it is in the documents section, and provides a great way to learn the basics of c++.
Posted 20 years ago2004-07-30 21:50:30 UTC Post #46913
I can combine, read and edit (to an extent) Will that do?
Posted 20 years ago2004-07-31 03:15:27 UTC Post #46977
Heh, I just wrote such a program, took me about 5 minutes. It's not so difficult, al I used was one while and one if statement.

What you do, is ask for an integer, then run through a while statement, and checking the remaining value of the (input % i), where i is an integer that increases each time the while is run, as long as (i <= input).
When this remaining value is 0, this means i is a factorial at that moment. Thus, you put this in an if statement, e.g. if(input % i ==0) { write factorials to console }.

That should be all you need to know...
Posted 20 years ago2004-07-31 03:55:33 UTC Post #46988
Check out the factorial example in section 2.3 of the cplusplus.com tutorial if you can't figure it out.
Posted 20 years ago2004-07-31 07:04:10 UTC Post #47040
Captain P. wrote:
where i is an integer that increases each time the while is run, as long as (i <= input).
Since the input is the product of two elements the sum of those two elements will always be smaller than (or equal to) 2*sqrt(input). Basically this means that when you do a run from 1 till input, you will be repeating steps after the square root of the input.

For example, the number 24 :

24 % 1 = 0 -> 1 is a valid element, 24 / 1 = 24
24 % 2 = 0 -> 2 is a valid element, 24 / 2 = 12
24 % 3 = 0 -> 3 is a valid element, 24 / 3 = 8
24 % 4 = 0 -> 4 is a valid element, 24 / 4 = 6
24 % 6 = 0 -> 6 is a valid element, 24 / 6 = 4 <- we had this one already!

So, instead of going through 1 to 24, you can stop at 4 which means about 86% gain in speed(!). Ofcourse this is a little less since you have to calculate the other factors(6, 8, 12, 24) as well which can be done by dividing the input with x where x = (input - x * n = 0) & (x < sqrt(input)) | (x E N).

As you might suspect, this especially comes in handy when factorizing large numbers since the time saved in percentage is about (100 - 100 / sqrt(input)). When we take input as infinitely large wel'll notice a 100% decrease in numbers to be calculated. Unfortunately at infinity you'll still have to calculate sqrt(infity) numbers which is an infinite amount :P.

Maybe interesting, even the fastest factorial algorithm delivers tons and tons of overhead. Computing the factorials of the product of two 512 bit primes takes more than 5 years on a distributed network! This characteristic behaviour is cleverly used in the RSA public/private key encryption system which makes it one of the safest encryption systems of the last 25 years.

Either way, here is some javacode on how to do it :P.
...
int input = 24;
int sqrt = (int)Math.sqrt((double)input); //take the root
for(int i = 0; i <= sqrt; i++) //do the loop
if ((input % i) == 0) //is the input dividable by the number?
    System.out.println(i + " / " + input / i); //then print the numbers
...
Posted 20 years ago2004-07-31 07:13:40 UTC Post #47047
Either way, here is some javacode on how to do it .
What's javacode?
Posted 20 years ago2004-07-31 07:15:04 UTC Post #47048
Java is a programming language obviously.
m0p m0pIllogical.
Posted 20 years ago2004-07-31 07:20:27 UTC Post #47051
Yeah I know what Java is but not necessarily javacode.
Posted 20 years ago2004-07-31 07:26:19 UTC Post #47052
I though about optimizing the script after I posted already, and you're definitely right Mike! Clever thinking! :)

Just changed my code and yes, that makes up for a lot less work indeed.
Posted 20 years ago2004-07-31 07:51:17 UTC Post #47054
hefsys wrote:
Yeah I know what Java is but not necessarily javacode.
Java is a programming language, and "code" is a part of a program so hence javacode, a part of a program which was written in Java. Either way, it was fairly easy recognizable by the use of the System package in combination with the used syntax ;).

ps: good moderation over there :cool:
Thanks - Seventh :)
Posted 20 years ago2004-08-04 09:52:44 UTC Post #47872
gheez thanks guys! ...I have to figure out some stuff but it helps me alot! i love you all! :D
You must be logged in to post a response.