Solved

scientific numbers in C++

Posted on 2007-12-07
8
570 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
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.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

19 Experts available now in Live!

Get 1:1 Help Now