Guest User

VeryMuch

a guest
Feb 16th, 2011
203
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<gmp.h>
  4.  
  5. int main( int argc, char** argv )
  6. {
  7.     int side = 100;
  8.     int maxlen = side*2+4;
  9.     int i, j, currentlen;
  10.    
  11.     printf( "No use of gmp.h up to this point. If this is the only thing that works it points to gmp.h\n" );
  12.  
  13.     mpz_t **currentlevel, **nextlevel, **templevel, result;
  14.     if( currentlevel = (mpz_t **)malloc(sizeof(mpz_t)) )
  15.     {
  16.         printf( "First malloc successful\n" );
  17.     }
  18.     if( nextlevel = (mpz_t **)malloc(sizeof(mpz_t)) )
  19.     {
  20.         printf( "Second malloc successful\n" );
  21.     }
  22.     if( *currentlevel = (mpz_t *)malloc(maxlen*sizeof(mpz_t)) )
  23.     {
  24.         printf( "Third malloc successful\n" );
  25.     }
  26.     if( *nextlevel = (mpz_t *)malloc(maxlen*sizeof(mpz_t)) )
  27.     {
  28.         printf( "Fourth malloc successful\n" );
  29.     }
  30.     printf( "All mallocs done!\n" );   
  31.  
  32.     printf( "Starting initialazation of mpz_t nums to zero\n" );
  33.     // initialize mpz_t nums to zero
  34.     for( i = 0; i < maxlen ; i++ )
  35.     {
  36.         mpz_init( *(*currentlevel+i) );
  37.         mpz_init( *(*nextlevel+i) );
  38.     }
  39.     // initialization done!
  40.     printf( "Initialization done!\n" );
  41.  
  42. /* <--- NOTA COMMENTIN TIL AÐ HINDRA NÆSTA KAFLA Í AÐ KEYRA
  43.     GETUR GERT TILRAUNIR MEÐ ÞETTA EF FORRIT KEYRIR OG
  44.     printf() TEXTAR PRENTA ÚT. MÁLIÐ AÐ ÞRENGJA RAMMAN UTAN UM VILLUNA
  45.     MEÐ ÚTILOKUNAR AÐFERÐINNI.
  46.  
  47.     MÉR ÞYKIR LÍKLEGT AÐ gmp.h HAFI EITTHVAÐ KLIKKAÐ Í UPPSETNINGU HJÁ ÞÉR
  48.     ÞÁ ÆTTI KEYRSLA AÐKLIKKA UM LEIÐ (EÐA FLJÓTLEGA) OG VITNAÐ ER Í ÞAÐ.
  49.                    
  50.     mpz_set_ui( *(*currentlevel+0), 1 ); // set first node to 1
  51.     currentlen = 1;
  52.     while( currentlen/2 <= side ) // currentlen/2 == currentlevel
  53.     {
  54.         i = 0;
  55.         j = currentlen-1;
  56.         while( i != j )
  57.         {   // i
  58.             mpz_add( *(*nextlevel+i), *(*nextlevel+i), *(*currentlevel+i) ); // S
  59.             mpz_add( *(*nextlevel+(i+1)), *(*nextlevel+(i+1)), *(*currentlevel+i) ); // SE
  60.             mpz_add( *(*currentlevel+(i+1)), *(*currentlevel+(i+1)), *(*currentlevel+i) ); // E
  61.  
  62.             // j
  63.             mpz_add( *(*currentlevel+(j-1)), *(*currentlevel+(j-1)), *(*currentlevel+j) ); // S
  64.             mpz_add( *(*nextlevel+(j+1)), *(*nextlevel+(j+1)), *(*currentlevel+j) ); // SE
  65.                     mpz_add( *(*nextlevel+(j+2)), *(*nextlevel+(j+2)), *(*currentlevel+j) ); // E
  66.             i++;
  67.             j--;
  68.         }
  69.         // middle
  70.         mpz_add( *(*nextlevel+i), *(*nextlevel+i), *(*currentlevel+i) ); // S
  71.         mpz_add( *(*nextlevel+(i+1)), *(*nextlevel+(i+1)), *(*currentlevel+i) ); // SE
  72.             mpz_add( *(*nextlevel+(i+2)), *(*nextlevel+(i+2)), *(*currentlevel+i) ); // E
  73.         mpz_set( result, *(*currentlevel+i) ); // save middle as possible result
  74.  
  75.         // swap levels, do not lose pointers to heap
  76.         templevel = currentlevel;
  77.         currentlevel = nextlevel;
  78.         nextlevel = templevel;
  79.  
  80.         // reset next level to zero
  81.         for( i = 0 ; i < maxlen ; i++ )
  82.         {
  83.             mpz_set_ui( *(*nextlevel+i), 0 );
  84.         }
  85.  
  86.         // increment currentlen
  87.         currentlen += 2;
  88.     }
  89.  
  90.         gmp_printf( "%Zd\n", result );
  91.  
  92. HÉR ENDAR KOMMENTIÐ SEM ÉG SETTI INN */
  93.  
  94.     // just for correctness not needed
  95.     free(*currentlevel);
  96.     free(currentlevel);
  97.     free(*nextlevel);
  98.     free(nextlevel);
  99.  
  100.     return 0;
  101. }
RAW Paste Data