![]() |
C++: Does this make any sense?
Well, so here we are on sixth list of programming homework. One of the tasks is to use a function to return the factorial of an input number.
As anyone with a shred of common sense can realise, normal variables won't do here: the factorial exceeds the capacity of such variable types as unsigned __int64 and long double after reaching the factorial of 20. I've had an idea how to get around that and just finished scribbling it down. The code is crude as fuck, but we won't get anything such as pointers explained before next lecture (which as after we're supposed to turn in the homework, so...) I'd like to ask if someone less n00bish in C++ than myself could take a look at the thing. Code:
#include <iostream.h> |
i havent looked into the code but you could either:
a) store number as a string b) store it as an array of int/char c) use an external library d) google for more solutions? and by the way.. why do you think you need to make a function able to handle really big numbers? is it in assignment? you're just beginners, maybe they meant `just make a function that does it`, don't care about big numbers. like up to 5 or so also, your code looks kind of messy, have you thought about defining the function via recursion? like this: Code:
unsigned long long int fact (int n){ or you could cut it down to this Code:
unsigned long long int fact (int n){ |
b) is the basic idea. I'm trying to cut it into an array of unsigned __int64's
Starting from the code I posted above, this is what I got: Code:
#include <cstdlib> Otherwise zeros start vanishing from various places in the number. |
why do you actually try to store them as an array of 64 bit ints? o_O
why not array of 0..9? |
Was trying to keep it in larger chunks than one digit each. That, obviously, didn't work out. :p
|
that would most probably three times more complex than defining your own multiplication function via array of digits
by the way, i installed this dev c++ right now and wrote the recursive version for you. try it.. trust me Code:
unsigned long long int fac(unsigned int n){ |
The teacher probably wants you to write your own multiplication function and return the result as a string. This is the only way to get it to be precise to an arbitrary length.
|
are you sure teacher wanted a function that ables to show factorial of large numbers like 100?
it is not possible in practical using with even unsigned long. i think it is also not possible to calculate it with forwarding it to string, you still need to have an integer for multiplication. there would be some mathematic libraries out there but i think your teacher don't want you to use them. there is an open source calculator speedcrunch, you can examine its codes for handling large numbers if you want. but it will be really painful. |
The only real requirement was to make it calculate a factorial, not actually return that to the parent function. The teacher is very insistent about every program having to output the results to the user, though.
This is what I ended up with - the program stores the number as nine-digit chunks in an array of unsigned integers. Code:
#include <cstdlib> The limit I imposed was an estimate based on calculation of the maximum size of monster array (anything over 520588 elements crashes the executable), multiplied by the number of digits per array element (nine) and divided by the proportion between the size of the factorial in calculation to its number of digits (for factorial of one million it's 5.565709 digits per one point of the number's size), thus: 841814. |
The current time is 05:09 PM (GMT) |
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.