Solved

scientific numbers in C++

Posted on 2007-12-07
8
572 Views
Last Modified: 2008-02-01
Hello group,

In a calculation I'm counting which gives result in scientifice number due to using Factorial such
26!/(26-4)! and ...

What varaible can I use? do I need to use template in C++? or how about PHP/JavaScript if you want to implement it in Web (either one)

Your help will be appreciated greatly.

Regards,
Amit
0
Comment
Question by:akohan
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20426351
printf("%d\n",25*24*23*21);
0
 
LVL 84

Expert Comment

by:ozo
ID: 20426357
Sorry, that should have been
26*25*24*23
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 250 total points
ID: 20426522
If you get paid by the line of code you write, you need to make it more complicated looking. Ozo's solution will pay you less money than the following, which is much harder to read and altogether less efficient.

If you can write this in a higher level language and make it even less efficient and complicated looking, you might even justify the budget for dedicated hardware.

[Dammit, I've been working for corporates for too long ;-) ]
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <numeric>
 
class DecrementorGen {
	int current;
public:
	DecrementorGen(int current) : current(current) {}
	int operator()() {
	        return current--;
	}
};
 
int main()
{
	std::vector<int> v(25-21);
	generate(v.begin(), v.end(), DecrementorGen(25));
	std::cout << accumulate(v.begin(), v.end(), 1, std::multiplies<int>()) << std::endl;
}

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 53

Expert Comment

by:Infinity08
ID: 20426597
>> What varaible can I use?

A normal 32bit int should be fine, assuming that you calculate it correctly. Do NOT try to store 26! in it ;) A 32bit int can't hold the result :) Calculate like ozo showed.


>> do I need to use template in C++?

I do not see why ? This is a simple calculation ... Or do you want to make your code generic to calculate x!/(x-y)! ??
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20426615
358800
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 20426636
>>>> Or do you want to make your code generic to calculate x!/(x-y)! ??

#ifdef WIN32
typedef __uint64 UInt64;
#else
typdef unsigned longlong UInt64;
#endif

 UInt64  calcYfromX(unsigned int x, unsigned int y)
 {
    UInt64 ui = 1;
    for (int i = 0; i < y; ++i)
           ui*=(x - i);
    return ui;
 }

Regards, Alex
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20426650
To be extra safe, you might want to check the input x and y values for overflow ... If the difference between them is too small, the result will not be correct.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 20426853
If the numbers are known at compile time you can use a template meta-function to calculate these. There are two advantages: -

1. You'll know at compile time if the result overflows the data type used (compile time warning)
2. The cost of calculation is at compile time and not runtime (as runtime they are just constant values)

I stress, this will only work if you know the values you wish to calculate the factorials for at compile time!

More reading: -

http://ubiety.uwaterloo.ca/~tveldhui/papers/Template-Metaprograms/meta-art.html
http://en.wikipedia.org/wiki/Template_metaprogramming

// A big type to hold factorial result
typedef unsigned long long bignum_t;
 
// Recursive template meta-function to calculate factorial at compile time
template <unsigned short N>
struct Factorial
{
	static bignum_t const value = N * Factorial<N - 1>::value;
};
 
// Specialized template meta-function to end recursion
template <>
struct Factorial<1>
{
	static bignum_t const value = 1;
};
 
int main()
{
	// This is calculated at compile time so there is no runtime cost
	bignum_t num = Factorial<10>::value; // Example for 10 (num == 3628800)
	return 0;
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question