Solved

scientific numbers in C++

Posted on 2007-12-07
8
588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

630 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