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
Solved

scientific numbers in C++

Posted on 2007-12-07
8
575 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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