Ejemplo n.º 1
0
void radix_sort(int * list, int list_size) {
  // Note: modifies list

  // find maximum elements
  int i;
  int k;
  int range = 10;
  int max = 0;
  for (i = 0; i < list_size; i++) {
    if (list[i] > max) {
      max = list[i];
    }
  }
  int max_digits = floor(log10(max) + 1);

  // allocate a second list that is the length of the original
  int *sorted_list = (int *) malloc(sizeof(int) * list_size);
  // allocate a third list that is the length of the original
  int counter_list[10] = {0}; 

  for (k = 0; k < max_digits; k++) {
    // Zero both lists
    for (i = 0; i < range; i++) {
      counter_list[i] = 0;
    }

    // Populate counter list and zero sorted_list
    for (i = 0; i < list_size; i++) {
      sorted_list[i] = 0;
      counter_list[ nth_digit(list[i],k) ]++; 
    }

    for (i = 1; i < range; i++) {
      counter_list[i] = counter_list[i] + counter_list[i-1];
    }

    for (i = 0; i < range; i++) {
      counter_list[i]--;
    }

    for (i = list_size-1; i >= 0; i--) {
      sorted_list[ counter_list[ nth_digit(list[i],k) ] ] = list[i];
      counter_list[ nth_digit(list[i],k) ]--; 
    }

    for (i = 0; i < list_size; i++) {
      list[i] = sorted_list[i]; 
    }
  }

  free(sorted_list);
}
Ejemplo n.º 2
0
int nth_champer (int i) {
    if (i<10) {
        return i;
    } else {
        int res = nth_digits (i);
        int fres = res % 10;
        int sres = res / 10;
        int rems = i - sres;
        int prevs, remss, remsss;
        if (0 == rems % fres) {
            prevs = (rems/fres) - 1;
            
        } else {
            prevs = (rems/fres);
        }
        
        remss = rems % fres;
        
        if (0 == remss) {
            remsss = fres -1;
        } else {
            remsss = remss - 1;
        }
        return nth_digit (pow(10,fres-1) + prevs , remsss);
    }
}
Ejemplo n.º 3
0
unsigned has_same_digits(unsigned a, unsigned b)
{
   unsigned digs = n_digits(a);
   if (digs == n_digits(b)) {
      unsigned digits[10] = {0};
      unsigned i;
      for (i=0;i<digs;i++)
         digits[nth_digit(a,i)]++;
      for (i=0;i<digs;i++) {
         if (digits[nth_digit(b,i)] == 0)
            return 0;
      }
      return 1;
   }
   return 0;
}
Ejemplo n.º 4
0
unsigned champernowne_digits_product(unsigned* indices){
	//indices is a 0-terminated array of strictly increasing integers
	unsigned product = 1;
	unsigned lower_value = 1;
	unsigned upper_value = 10;
	
	unsigned lower_index = 1;
	unsigned delta_index = 9;
	unsigned num_digits  = 1;
	unsigned upper_index = 10;
	
	for (unsigned* target_index = indices; *target_index > 0; target_index++){
		while (*target_index >= upper_index){
			lower_value = upper_value;
			upper_value *= 10;
			
			lower_index = upper_index;
			num_digits++;
			delta_index *= 10;
			upper_index += delta_index * num_digits;
		}
		
		unsigned value  = lower_value + (*target_index - lower_index) / num_digits;
		unsigned offset = 1 + (*target_index - lower_index) % num_digits;
		
		product *= nth_digit(value, 10, offset);
	}
	return product;
}
Ejemplo n.º 5
0
long sum_of_factorial_digits(int num) {
	
	long sum = 0;
	for(int n = 0; n <= (int)floor(log(num)/log(10)); n++) {
		sum += factorial[nth_digit(num, n)];
	}
	return sum;
	
}
Ejemplo n.º 6
0
int item_nth_digit(item i, int d) {
    return nth_digit(i.key.number, d);
}