Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Look and say sequence calculator by Braden Best
- * Inspired by a numberphile video: https://www.youtube.com/watch?v=ea7lJkEhytA
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFSZ 100000
- void
- build(char *buf)
- {
- char buf2[BUFSZ] = "",
- *buf2_p = buf2,
- temp;
- int digit,
- count;
- strncpy(buf2, buf, strlen(buf));
- while(*buf2_p){
- temp = *buf2_p;
- count = 0;
- while(*buf2_p && *buf2_p == temp){
- buf2_p++;
- count++;
- }
- digit = temp - '0';
- snprintf(buf, 3, "%i%i", count, digit);
- buf += 2;
- }
- }
- void
- look_and_say(int n, int lim)
- {
- char buf[BUFSZ] = "";
- int i;
- printf("Seed: %i\n\n", n);
- snprintf(buf, BUFSZ, "%i", n);
- for(i = 1; i <= lim; i++){
- build(buf);
- printf("Generation %i: %s\n\n", i, buf);
- }
- }
- int
- main(int argc, char **argv)
- {
- if(argv[1] && argv[2]){
- look_and_say(atoi(argv[1]), atoi(argv[2]));
- } else {
- printf( "Usage: %s [starting point] [number of generations]\n"
- "Try %s 1 7\n",
- argv[0],
- argv[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement