PDA

View Full Version : nCr Engine(C++) + A joke


madurax86
02-28-2009, 12:31 PM
Full code is below its not optimized for speed if you can post the optimizations that can be implemented, heres a LOL (http://1.bp.blogspot.com/_793N2ay-vhU/Sag6kAeuPDI/AAAAAAAAABQ/S7g3On38O8s/s1600/lol.JPG) that i came across when i was coding this last night.

Any Problems comments are welcome; there are a lot of C programmers in EK if you can translate this to C :) there are very few changes to be made ryt?

/*
factorial and nCr functions(not optimized)
==========================================
by madura.x86
comment: If you are getting minus values for a given nCr its because "long long"
type isnt enough try raising it to double, nothing wrong with the engine the
values get awfully longer when n goes just after 51, have fun with it but dont
just copy give credit where credit is due.
*/
#include <cstdlib>
#include <iostream>

using namespace std;
double fact(double i){
double j,k;
k=1;
for (j=1;j<=i;j++) k=k*j;
return k;
}
long long nCr(int n, int r){
int j,i,k,m1,n1;
long long p,q;
long long x[128],y[128];
if (r>n) return 0;
if (r==n) return 1;
if (r==0) return 1;
if (n<=2*r){
k=0;
for (j=n-r+1;j<=n;j++){
k++;
x[k]=j;
}
m1=k;
k=0;
for (i=2;i<=r;i++){
k++;
y[k]=i;
}
n1=k;
}
else
{
k=0;
for (j=r+1;j<=n;j++){
k++;
x[k]=j;
}
m1=k;
k=0;
for (i=2;i<=n-r;i++){
k++;
y[k]=i;
}
n1=k;
}

for (j=1;j<=m1;j++)
for (i=1;i<=n1;i++){
if (x[j]%y[i]==0){
x[j]=x[j]/y[i];
y[i]=1;}

if (y[i]%x[j]==0){
y[i]=y[i]/x[j];
x[j]=1;}
}
p=1;
q=1;
for (j=1;j<=m1;j++){
if (x[j]!=1) p=p*x[j];}
for (j=1;j<=n1;j++){
if (y[j]!=1) q=q*y[j];
}
return p/q;
}

int main(int argc, char *argv[]){
int i,j;
j=51;
for (i=0;i<j+1;i++){
cout<<j<<"C"<<i<<" = "<<nCr(j,i)<<endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}

madurax86
02-28-2009, 12:39 PM
results for n=51 highest that this thing can produce; for n>51 we have to use double but it outputs in scientific notation so its approx.
http://i433.photobucket.com/albums/qq55/madurax86/saa.jpg

madurax86
02-28-2009, 01:34 PM
katawath epaaaaaa do???

HIVOLTAG3
03-01-2009, 11:37 AM
mm a scientific jork...:D

madurax86
03-01-2009, 01:34 PM
mm a scientific jork...:D
ambe ekek wath reply kora!! hehe mona science da ban me simple math ne podak fast wena sulu karana system ekak damma normal factorial function walin nCr gananaya karana ba factorial function eka double return karana dammath nCr walata precision nati wenawa samahara welawata

results of normal nCr function using the fact() function n=51;
http://i433.photobucket.com/albums/qq55/madurax86/sd-1.jpg
Sure it can go for more than 51 but precision isnt there because they tend to use scientific notation
Blogspot hates hot linking i just learned that !!
http://i433.photobucket.com/albums/qq55/madurax86/lol.jpg

HIVOLTAG3
03-01-2009, 02:29 PM
ambe ekek wath reply kora!! hehe mona science da ban me simple math ne podak fast wena sulu karana system ekak damma normal factorial function walin nCr gananaya karana ba factorial function eka double return karana dammath nCr walata precision nati wenawa samahara welawata

results of normal nCr function using the fact() function n=51;
http://i433.photobucket.com/albums/qq55/madurax86/sd-1.jpg
Sure it can go for more than 51 but precision isnt there because they tend to use scientific notation
Blogspot hates hot linking i just learned that !!
http://i433.photobucket.com/albums/qq55/madurax86/lol.jpg

ya itz nice to hv higher precision in everywhr. and thnx for the info mate..

but in practice,most of the time no 1 go after the 10^10+ numbers, so itz convinient to express them as a power of 10.

anyway nice to hv this kind of methodz ..:)

madurax86
03-01-2009, 02:33 PM
ya itz nice to hv higher precision in everywhr. and thnx for the info mate..

but in practice,most of the time no 1 go after the 10^10+ numbers, so itz convinient to express them as a power of 10.

anyway nice to hv this kind of methodz ..:)

man nikan athal ekata ban haduwe ona ayata badu have ne ethota ...im gonna make a class for multiplying,dividing,adding,subtracting that will calculate the number as we do then we wont have to rely on normal double precision :P all numbers will be char arrays
check out the Calc in my blog a friend of mine coded a similar class in VB for that Calc :P 2048 digit precision :D me karana wada nathuwata karana ewa ban :rofl:

HIVOLTAG3
03-01-2009, 02:37 PM
man nikan athal ekata ban haduwe ona ayata badu have ne ethota ...im gonna make a class for multiplying,dividing,adding,subtracting that will calculate the number as we do then we wont have to rely on normal double precision :P all numbers will be char arrays
check out the Calc in my blog a friend of mine coded a similar class in VB for that Calc :P 2048 digit precision :D me karana wada nathuwata karana ewa ban :rofl:
:P :P

Wolverine GTR
03-01-2009, 04:26 PM
He He.....
Nice Joke!!!!!!!!!

chamidilk
03-01-2009, 04:43 PM
mokakda oke joke eka malayo.....
man nam ithin oya kagewath prog edit karanna nam yanne naha

Im.Just.a.Fool
04-05-2009, 01:39 PM
parana thread awussala baladdi meka ahu une :D (mama thama awa witharaine). I wrote 3 methods to calculate nCr. ekin eka dannam. Java walin liwwe.

Im.Just.a.Fool
04-05-2009, 01:42 PM
// The simplest way to calculate nCr, using factorial function.
// This can calculate nCr upto n = 20.

long fact(int n) {
long x = 1;
for (int i = 2; i <= n; i++) x *= i;
return x;
}

long nCr(int n, int r) {
return fact(n) / fact(r) / fact(n-r);
}

Im.Just.a.Fool
04-05-2009, 01:44 PM
// Another method to calculate nCr, this method can calculate
// upto n = 29.

long nCr(int n, int r) {
if (r > n / 2) r = n - r;
long x = 1;
for (int i = n; i > n-r; i--) x *= i;
for (int i = 2; i <= r; i++) x /= i;
return x;
}

Im.Just.a.Fool
04-05-2009, 01:48 PM
// This method can calculate nCr upto n = 66. The largest value for nCr
// that can be stored in a Java long variable is 7,219,428,434,016,265,740
// (for n = 66, r = 33). The range of the type long in Java is from
// -2^63 to 2^63-1. This method uses an array to store the prime factors
// of the numbers that are multiplied and divided.

long nCr(int n, int r) {
int[] pf = new int[n+1]; // n+1 is the length of the array pf.
for (int i = n; i > n-r; i--) {
int x = i;
int j = 2;
while (x >= j) {
while (x % j == 0) {
pf[j]++;
x /= j;
}
j++;
}
}
for (int i = 2; i <= r; i++) {
int x = i;
int j = 2;
while (x >= j) {
while (x % j == 0) {
pf[j]--;
x /= j;
}
j++;
}
}
long res = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= pf[i]; j++) res *= i;
}
return res;
}

Im.Just.a.Fool
04-05-2009, 01:58 PM
mamath meka wena karanna deyak nathi kamata liwwe :D. EK eke programming karana aya (both professionals and students) godak athine. ea ayath comments danawanam godak hondai :yes:.

spgun
04-05-2009, 02:11 PM
Cordings r too long..

Im.Just.a.Fool
04-05-2009, 02:15 PM
Cordings r too long..


If you can code a more efficient algorithm that can calculate nCr for larger values of n, please post it. :)

Im.Just.a.Fool
04-05-2009, 02:17 PM
spgun,

Ok nevermind if it is efficient or not. oya kamathi onama widiyataka hadala post karanna. Then we can discuss about it.

spgun
04-05-2009, 02:17 PM
I'll post it tomorrow.It's not with me right now.OK

Im.Just.a.Fool
04-05-2009, 02:23 PM
I'll post it tomorrow.It's not with me right now.OK

OK. :)

Im.Just.a.Fool
04-05-2009, 03:15 PM
ko wena kauruth nadda? :(

madurax86
04-05-2009, 10:30 PM
innawooo
maath dan dakke

madurax86
04-05-2009, 10:35 PM
Java is abit high level on variables we can code somthing similar to that in vb6(using strings and it'll have a very high storage) but speed gets lower so i stayed with c++, in this one nCr is not only being calculated; the simplifying of factorials is done thru the code too:P

charmer
04-05-2009, 10:49 PM
hi mate,
can u make a random number generator which predticts the next drawing number (between 1-40) according to the previous 10 drawn numbers for bingo / lotto games please

Muzain
04-05-2009, 10:50 PM
Greek Basawa Mata Hodaaa

madurax86
04-05-2009, 11:05 PM
hi mate,
can u make a random number generator which predticts the next drawing number (between 1-40) according to the previous 10 drawn numbers for bingo / lotto games please

you can code it your self
open up vb6
draw a button, double click on it to goto code view
and put this code in

Randomize
MsgBox (Fix(Rnd * 39)+1)


+1 is used to remove outputing 0 :P
remove the (Fix(Rnd*39)+1) and put rnd*40 if you want real output too(this will only give integer output)

harintheman
04-05-2009, 11:07 PM
ela jokke

jeffhardy
04-05-2009, 11:18 PM
ela ela la

Im.Just.a.Fool
04-06-2009, 12:04 AM
Java is abit high level on variables we can code somthing similar to that in vb6(using strings and it'll have a very high storage) but speed gets lower so i stayed with c++, in this one nCr is not only being calculated; the simplifying of factorials is done thru the code too:P

What is the range of long long in C++?

If the range is from -2^63 to 2^63-1, the program I posted in #14 should work well when converted into C++.

madurax86
04-06-2009, 12:14 AM
What is the range of long long in C++?

If the range is -2^64 to 2^64-1, the program I posted in #14 should work well when converted into C++.

dont know ban :P i just put it because double tends to use scientific notation at 6 numbers(i think) nCr needs precision more than that ne so I used the long long, and long long long turned out to be the joke :D
Do you use the factorial simplification in ur code?
like, nCr=fact(n)/fact(r)*fact(n-r)

if (r>n-r),
nCr=[(r+1)(r+2)(r+3)......(n-1)(n)]/(n-r)!

if (r<n-r),
nCr=[(n-r+1)(n-r+2)(n-r+3)......(n-1)(n)]/(n-r)!

for another simplification do the integer dividing too, I did that in my code. If java has more variable length then use these too then the output will be much larger :D and faster

nagaya
04-06-2009, 12:26 AM
64bit ta compile kare long long daan :P :P

madurax86
04-06-2009, 12:32 AM
64bit ta compile kare long long daan :P :P
apo na 32bit :P
long long wada

Im.Just.a.Fool
04-06-2009, 12:37 AM
Do you use the factorial simplification in ur code?


I did the following:

n! / (r! * (n-r)!) = (n * (n-1) * ... * (n-r+2) * (n-r+1) ) / (1 * 2 *...*(n-1) * n)

E.g. For n = 50 and r = 24,
50! / (24! * 26!) = (50 * 49 *...*28 * 27) / (1 * 2 * ...* 23 * 24)

ita passe (50 * 49 *...*28 * 27) wala prathamaka sadaka hoyala pf array eke values increment karanawa. E.g. number eka 24 nam pf[2] thun parak increment wenawa, pf[3] eka parak increment wenawa.

ita passe (1 * 2 * ...* 23 * 24) wala prathamaka sadaka hoyala ewa pf array eke danata thiyena values walin adu karanawa.

anthimata pf array eke ithuru wenne nCr wala prathamaka sadaka tika. ea tika wadi karala nCr calculate karanawa.

Im.Just.a.Fool
04-06-2009, 12:43 AM
kohoma hari C++ wala long long variable eka 32 bit walata wada wadi wenna one.

nathnam nCr(51,25) = 247,959,266,474,052 value eka store karala thiyaganna bahane :D

247,959,266,474,052 store karanna adu gane 48 bits one :)

Im.Just.a.Fool
04-06-2009, 12:44 AM
64bit ta compile kare long long daan :P :P


sadaadara nayo oyath nCr walata function ekak liyala post karannako :D

madurax86
04-06-2009, 12:53 AM
kohoma hari C++ wala long long variable eka 32 bit walata wada wadi wenna one.

nathnam nCr(51,25) = 247,959,266,474,052 value eka store karala thiyaganna bahane :D

247,959,266,474,052 store karanna adu gane 48 bits one :)

machine eka nam 64bit capable eth windows xp 32bit professional ne hari koma hari output wenawa ne :P
oyage krame hodai prathamaka saadaka walata man balanne haraye tiyena numbers walin lawaye eka number ekak hari bedenawa nam purna sankyawak denna bedala harayai lawayai wenama sulu karala passe bedhanawa :D

Im.Just.a.Fool
04-06-2009, 01:07 AM
machine eka nam 64bit capable eth windows xp 32bit professional ne hari koma hari output wenawa ne :P
oyage krame hodai prathamaka saadaka walata man balanne haraye tiyena numbers walin lawaye eka number ekak hari bedenawa nam purna sankyawak denna bedala harayai lawayai wenama sulu karala passe bedhanawa :D


ah ow eka thamai oyage program eken karanne. For e.g. for nCr(51, 25)

x = 29 31 35 37 39 41 43 9 47 4 49 10 51
y = 21 25 26

kiyana values thamai ithuru wenne. anith numbers okkoma are nested for loops deken sulu karaddi kapila kapila yanawa :D


n = 52 weddi x wala numbers wadi karaddi p variable eka overflow wenawa. ethakota thamai output eka waradinne

madurax86
04-06-2009, 01:12 AM
ah ow eka thamai oyage program eken karanne. For e.g. for nCr(51, 25)

x = 29 31 35 37 39 41 43 9 47 4 49 10 51
y = 21 25 26

kiyana values thamai ithuru wenne. anith numbers okkoma are nested for loops deken sulu karaddi kapila kapila yanawa :D


n = 52 weddi x wala numbers wadi karaddi p variable eka overflow wenawa. ethakota thamai output eka waradinne

yup eka thama 51ta witharak thibbe
wena kramyak ahu une na thawa optimize karanna oya range ekatama

Im.Just.a.Fool
04-06-2009, 01:17 AM
yup eka thama 51ta witharak thibbe
wena kramyak ahu une na thawa optimize karanna oya range ekatama


machan are you a university student or still schooling?

mata business programming wadiya allanne naha :no:. me wage ewatanam kamathi :D


But I'm just a fool so moley bohoma aduwen thamai wada karanne :rofl:. padam karannath hari kammali. godak aya danna dewal walin bagayakwath mama danne na.

Scientific programming/System programming karana ayata lankawe jobs thiyenawada?

madurax86
04-06-2009, 01:25 AM
machan are you a university student or still schooling?

mata business programming wadiya allanne naha :no:. me wage ewatanam kamathi :D


But I'm just a fool so moley bohoma aduwen thamai wada karanne :rofl:. padam karannath hari kammali. godak aya danna dewal walin bagayakwath mama danne na.

Scientific programming/System programming karana ayata lankawe jobs thiyenawada?

just a student still :P thama uni giye naa
ehema hithanna epaa programming walata moleta wadaa one creativity im sure you can find a better job if you dont get on to the normal business/database based programming courses i recon UCSC post graduate karanna puluwan nam hodai habai un uth lankawe anith aya wagema thama unicode wade patan gaththama mata hodata theruna unge hatith(only the profs and academic staff not the students) reply ekak wath naa mail ekata anthimedi manma thaniyen hoya gaththaa :P ohoma thamaa
goto my blog for more info on that
if want a new release of the prog download this the one in the blog is abit old
this one has a bug too it cant type numbers :P fixed that i'll up it later, im going now tc
http://madurax86.comuv.com/Unicode.zip

Im.Just.a.Fool
04-06-2009, 01:27 AM
ok machan mamath dana yanawa. Good night.

kasuncs
04-06-2009, 01:49 AM
int64