Solved

scientific numbers in C++

Posted on 2007-12-07
8
571 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now