Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Rollback to Revision 3
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

#include <stdio.h> #include <string.h> #include <stdlib.h> #MAX_WORD_LENGTH 30 // don't think the number could get so long typedef unsigned long long ull; void conv(ull num, const char *alpha, char *word, int base){ while (num) { *(word++)=alpha[(num-1)%base]; num=(num-1)/base; } } int main(){ ull nu; const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34"; /* "OSR43" was added to show that the letters of alpha are NOT in alphabetical order */ char *word=calloc(MAX_WORD_LENGTH30,sizeof(char)); // word always contains null-terminator int base=(int)strlen(alpha); for (nu=1;nu<=1e8;++nu) { conv(nu,alpha,word,base); printf("%s\n",strrev(word)); } return 0; } 

This code's working fine but I need to speed it up as much as possible. How do I do it?

EDIT:

I forgot to add strrev() to reverse the result string, edited now.

The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

#include <stdio.h> #include <string.h> #include <stdlib.h> #MAX_WORD_LENGTH 30 // don't think the number could get so long typedef unsigned long long ull; void conv(ull num, const char *alpha, char *word, int base){ while (num) { *(word++)=alpha[(num-1)%base]; num=(num-1)/base; } } int main(){ ull nu; const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34"; /* "OSR43" was added to show that the letters of alpha are NOT in alphabetical order */ char *word=calloc(MAX_WORD_LENGTH,sizeof(char)); // word always contains null-terminator int base=(int)strlen(alpha); for (nu=1;nu<=1e8;++nu) { conv(nu,alpha,word,base); printf("%s\n",strrev(word)); } return 0; } 

This code's working fine but I need to speed it up as much as possible. How do I do it?

EDIT:

I forgot to add strrev() to reverse the result string, edited now.

The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

#include <stdio.h> #include <string.h> #include <stdlib.h> typedef unsigned long long ull; void conv(ull num, const char *alpha, char *word, int base){ while (num) { *(word++)=alpha[(num-1)%base]; num=(num-1)/base; } } int main(){ ull nu; const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34"; /* "OSR43" was added to show that the letters of alpha are NOT in alphabetical order */ char *word=calloc(30,sizeof(char)); // word always contains null-terminator int base=(int)strlen(alpha); for (nu=1;nu<=1e8;++nu) { conv(nu,alpha,word,base); printf("%s\n",word); } return 0; } 

This code's working fine but I need to speed it up as much as possible. How do I do it?

added 12 characters in body; added 82 characters in body
Source Link
ForceBru
  • 455
  • 2
  • 10

The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

#include <stdio.h> #include <string.h> #include <stdlib.h> #MAX_WORD_LENGTH 30 // don't think the number could get so long typedef unsigned long long ull; void conv(ull num, const char *alpha, char *word, int base){ while (num) { *(word++)=alpha[(num-1)%base]; num=(num-1)/base; } } int main(){ ull nu; const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34"; /* "OSR43" was added to show that the letters of alpha are NOT in alphabetical order */ char *word=calloc(30MAX_WORD_LENGTH,sizeof(char)); // word always contains null-terminator int base=(int)strlen(alpha); for (nu=1;nu<=1e8;++nu) { conv(nu,alpha,word,base); printf("%s\n",strrev(word)); } return 0; } 

This code's working fine but I need to speed it up as much as possible. How do I do it?

EDIT:

I forgot to add strrev() to reverse the result string, edited now.

The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

#include <stdio.h> #include <string.h> #include <stdlib.h> typedef unsigned long long ull; void conv(ull num, const char *alpha, char *word, int base){ while (num) { *(word++)=alpha[(num-1)%base]; num=(num-1)/base; } } int main(){ ull nu; const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34"; /* "OSR43" was added to show that the letters of alpha are NOT in alphabetical order */ char *word=calloc(30,sizeof(char)); // word always contains null-terminator int base=(int)strlen(alpha); for (nu=1;nu<=1e8;++nu) { conv(nu,alpha,word,base); printf("%s\n",strrev(word)); } return 0; } 

This code's working fine but I need to speed it up as much as possible. How do I do it?

EDIT:

I forgot to add strrev() to reverse the result string.

The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number can consist.

The number to convert is always increasing, like from 1 to 45789, not from 536 to 13. The resulting number may not contain zero as first digit, so I need to carry the one.

#include <stdio.h> #include <string.h> #include <stdlib.h> #MAX_WORD_LENGTH 30 // don't think the number could get so long typedef unsigned long long ull; void conv(ull num, const char *alpha, char *word, int base){ while (num) { *(word++)=alpha[(num-1)%base]; num=(num-1)/base; } } int main(){ ull nu; const char alpha[]="abcdefghijklmnopqrstuvwxyzOSR34"; /* "OSR43" was added to show that the letters of alpha are NOT in alphabetical order */ char *word=calloc(MAX_WORD_LENGTH,sizeof(char)); // word always contains null-terminator int base=(int)strlen(alpha); for (nu=1;nu<=1e8;++nu) { conv(nu,alpha,word,base); printf("%s\n",strrev(word)); } return 0; } 

This code's working fine but I need to speed it up as much as possible. How do I do it?

EDIT:

I forgot to add strrev() to reverse the result string, edited now.

added 77 characters in body
Source Link
ForceBru
  • 455
  • 2
  • 10
Loading
added 108 characters in body
Source Link
ForceBru
  • 455
  • 2
  • 10
Loading
edited title
Link
ForceBru
  • 455
  • 2
  • 10
Loading
Source Link
ForceBru
  • 455
  • 2
  • 10
Loading