Total topics: 3
The Birthday Problem:
If I am partying in a room with 25 people; what is the chance that NO two of those people will share a birthday?
Another way to phrase this: When given 25 random people, what is the chance that for every day of the year, either one or zero of those people will have a birthday on that day.
The Birthday Solution:
For simplicity's sake, we will assume that all days of the year are equally likely to have birthdays fall on them.
For simplicity's sake, we will also ignore leap years, and thus leap days.
Since each person could have a birthday on any day of the year, in total there are 365 ^ 25 possibilities.
But how many of those 365 ^ 25 possibilities will satisfy the requirement that no two people may share a birthday?
You can think of it like this: Person-A has 356 days to choose from. Person-B only has 364 days to choose from without conflicting with person-A's choice. Person-C has 363 choices, and so on and so forth.
Right now we have two numbers: 365 * 364 * 363 . . . * 343 * 342 * 341 simplifies to 365! / 340! which is the number of possibilities that work, and 365 ^ 25 is the total number of possibilities.
By using division, and then multiplying by 100, we come to a percentage: 43.18%
The Birthday Problem 2.0:
Because everybody likes me, I am partying in a room with 170 people; what is the chance that NO four people will share a birthday?
Another way to phrase this: When given 170 random people, What is the chance that for every day of the year, no more than three of those people will have a birthday on that day.
The Birthday Solution 2.0:
I was going to try to explain my program, but I am not that good at explaining things, so here is the C code.
int stopfact( int pier, int depth ) { int retval = 1; for( int i = 0; i < depth; ++i ) { retval *= pier; --pier; } return retval;}double power( double base, int exponent ) { double retval = 1.0; for( int i = 0; i < exponent; ++i ) { retval *= base; } return retval;}#include <stdio.h>#include <stdlib.h>int main() { int bucket_count = 0; double * buckets; double chance_total = 0.0; int copies; double frequency; while( scanf( "%i %lf", & copies, & frequency ) == 2 ) { buckets = realloc( buckets, sizeof( double ) * ( bucket_count + copies ) ); for( int i = 0; i < copies; ++i ) { buckets[ bucket_count ] = frequency; ++bucket_count; chance_total += frequency; } } int balls; int limit; double * chancetable; double * newtable; scanf( "?> %i %i", & balls, & limit ); chancetable = malloc( sizeof( double ) * ( balls + 1 ) ); newtable = malloc( sizeof( double ) * ( balls + 1 ) ); chancetable[ 0 ] = 1.0; for( int i = 1; i <= balls; ++i ) { chancetable[ i ] = 0.0; } for( int i = 0; i < bucket_count; ++i ) { for( int u = 0; u <= balls; ++u ) { newtable[ u ] = 0.0; } for( int u = 0; u < limit; ++u ) { for( int z = 0; z + u <= balls; ++z ) { newtable[ z + u ] += chancetable[ z ] * ( double )( stopfact( balls - z, u ) / stopfact( u, u ) ) * power( buckets[ i ] / chance_total, u ) * power( ( chance_total - buckets[ i ] ) / chance_total, ( balls - z ) - u ); } } for( int u = 0; u <= balls; ++u ) { chancetable[ u ] = newtable[ u ]; } chance_total -= buckets[ i ]; } printf( "%lf %\n", chancetable[ balls ] * 100.0 ); free( buckets ); free( chancetable ); return 0;}
To see the answer for yourself, run the program and input the following:
365 41 1?> 170 4
If you made sure to space separate, the result should be: 61.30% accounting for leap years too.
Created:
Updated:
Category:
Technology
For those of you that don't/won't include basic math libraries in your programs, you probably do something like this for your root functions:
double sqrt( double whole ) {double accumulator = 1.0;double difference = whole - 1.0;for( int i = 0; i < 54; ++i ) {difference /= 2.0;if( whole < SQUARE( accumulator ) == whole < SQUARE( accumulator + difference ) ) {accumulator += difference;}}return accumulator;}
Yucky. Objects are cool, so join the crowd. Here is an example program, which shows how you can just shove all the computation up front.
#define ROOTSET 2 #include <stdlib.h>double power( double base, long exponent ) { double scraper = 1.0; while( exponent != 1 ) { if( exponent % 2 == 1 ) { scraper *= base; } base *= base; exponent /= 2; } return base * scraper;}typedef struct { double spacing; long tablesize; double * table;} RootTable;RootTable roottable_init( long mode, double spacing, long tablesize ) { RootTable retval; retval . spacing = power( spacing, mode ); retval . tablesize = tablesize; retval . table = malloc( sizeof( double ) * tablesize ); long index = 0; for( long i = 0; ; ++i ) { for( long u = 0; u < power( i + 1, mode ) - power( i, mode ); ++u ) { retval . table[ index ] = spacing * ( ( double )( i ) + ( double )( u ) / ( double )( power( i + 1, mode ) - power( i, mode ) ) ); if( ++index >= tablesize ) { return retval; } } }}#define ISPOS( X ) ( X + X > X )double roottable_get( RootTable self, double searchval ) { if( ISPOS( searchval ) && ( long )( searchval / self . spacing ) < self . tablesize ) { return self . table[ ( long )( searchval / self . spacing ) ]; } else { return 0.0; }}void roottable_free( RootTable self ) { free( self . table );}#include <stdio.h>int main() { double spacing; long tablesize; scanf( "%lf %ld", & spacing, & tablesize ); RootTable subject = roottable_init( ROOTSET, spacing, tablesize ); while( 1 ) { double searchval; if( scanf( "%lf", & searchval ) == 1 ) { printf( "%lf\n", roottable_get( subject, searchval ) ); } else { roottable_free( subject ); printf( "End Program\n" ); return 0; } }}
Only braindead apes use functional programming in the big 25. Look at how smart I am, using objects in my C code.
Created:
Updated:
Category:
Technology
If you compare Java to its rivals within its own family of object-oriented programming, such as C# there is nothing that is more sensible, straightforward or compact in java than in C# (including making edits later on in the coding).
It is fucking STUPID!
If you now compare Java to non-object-oriented languages like Python... Wow, Java is blatantly shit.
Whoever thought Java is a sensible language has no idea how to strategise a coding language where you don't declare all variable fields at the start and don't need to change the code in 2-3 different places every damn time you want to make a single, minor alteration.
How can Java still be lasting when C-languages thwart it at its own game?
Created:
Updated:
Category:
Technology