Skip to main content
Commonmark migration
Source Link

#C/bc, 233 121 112 bytes (assuming 3 byte penalty for |bc)

C/bc, 233 121 112 bytes (assuming 3 byte penalty for |bc)

#C/bc, 233 121 112 bytes (assuming 3 byte penalty for |bc)

C/bc, 233 121 112 bytes (assuming 3 byte penalty for |bc)

Original is in the edit history, no need to show it.
Source Link
rici
  • 691
  • 4
  • 7

For historical interest, the original hacky, ungolfedThis computed F(5000) -- an 18, warning592-free version:digit number -- in under 10 seconds.

#include<stdio.h>
// In the golfed version, C is [58] to avoid subtracting z
int C[10]={1},n=1,z='0';
int main(int c,char**v){
  FILE*f=popen("bc","w");
  // This could have been char*p=strchr(*++v,0); but
  // that would require another include.
  char*p=*++v;while(*p)++p;
  // *v always points to the beginning of the number with
  // leading 0s removed. So when the last leading zero
  // is gone, we're done. Before we start, we skip over all
  // leading zeros (including the only 0 if that's the argument).
  // This line is not in the golfed version.
  while(**v==z)++*v;
  while(**v){
    // In the golfed version, the condition is *--p==z&&p!=*v
    // to avoid a problem if the argument was 0. But that's not
    // necessary here, because leading zeros have$ beentime eliminated.
    // This subtracts one from the decimal representation of the number.
  cg70892g3<<<5000|BC_LINE_LENGTH=0 bc|wc while(*--p==z)*p+=9;--*p;
    // If the first digit of the number is now 0, move over it.
    if(**v<=z)++*v;
    // Now, we just count each digit, emitting the appropriatec
    // multiply and divide.18593
    for(p=*v;*p;fprintf(f,"%d/%d*",++n,++C[*p++-z]));
    // At the end of the above, p is againreal pointing to the trailing NUL0m9.
  }
  // The formula (if one was produced) ends with a '*', so we
  // need to fill in an identity, and add the return to trigger274s
user  // the computation0m9.
  fputs("1\n",f);
  // This line is absolutely required for conformant execution; without
  // it, the 'bc' processed is orphaned before it finishes and the output273s
  // willsys be intermingled with the next command0m0.
  return fclose(f);
}005s

For historical interest, the original hacky, ungolfed, warning-free version:

#include<stdio.h>
// In the golfed version, C is [58] to avoid subtracting z
int C[10]={1},n=1,z='0';
int main(int c,char**v){
  FILE*f=popen("bc","w");
  // This could have been char*p=strchr(*++v,0); but
  // that would require another include.
  char*p=*++v;while(*p)++p;
  // *v always points to the beginning of the number with
  // leading 0s removed. So when the last leading zero
  // is gone, we're done. Before we start, we skip over all
  // leading zeros (including the only 0 if that's the argument).
  // This line is not in the golfed version.
  while(**v==z)++*v;
  while(**v){
    // In the golfed version, the condition is *--p==z&&p!=*v
    // to avoid a problem if the argument was 0. But that's not
    // necessary here, because leading zeros have been eliminated.
    // This subtracts one from the decimal representation of the number.
    while(*--p==z)*p+=9;--*p;
    // If the first digit of the number is now 0, move over it.
    if(**v<=z)++*v;
    // Now, we just count each digit, emitting the appropriate
    // multiply and divide.
    for(p=*v;*p;fprintf(f,"%d/%d*",++n,++C[*p++-z]));
    // At the end of the above, p is again pointing to the trailing NUL.
  }
  // The formula (if one was produced) ends with a '*', so we
  // need to fill in an identity, and add the return to trigger
  // the computation.
  fputs("1\n",f);
  // This line is absolutely required for conformant execution; without
  // it, the 'bc' processed is orphaned before it finishes and the output
  // will be intermingled with the next command.
  return fclose(f);
}

This computed F(5000) -- an 18,592-digit number -- in under 10 seconds.

$ time ./cg70892g3<<<5000|BC_LINE_LENGTH=0 bc|wc -c
18593

real    0m9.274s
user    0m9.273s
sys     0m0.005s
added 444 characters in body
Source Link
rici
  • 691
  • 4
  • 7

Ungolfed and warning free:

#include <stdio.h>
int main() {
  int C[10]={1},n=1,k,t;    /* 0 is special-cased */
  for(scanf("%d",&k);k--;)  /* For each integer less than k */
    for(int t=k;t;t/=10)    /* For each digit in t */
      printf("%d/%d*",++n,++C[t%10]);  /* Incremental choice computation */
  puts("1");                /* Finish the expression */
}

Illustrated (which I trust shows the algorithm):

Illustrated:

Ungolfed and warning free:

#include <stdio.h>
int main() {
  int C[10]={1},n=1,k,t;    /* 0 is special-cased */
  for(scanf("%d",&k);k--;)  /* For each integer less than k */
    for(int t=k;t;t/=10)    /* For each digit in t */
      printf("%d/%d*",++n,++C[t%10]);  /* Incremental choice computation */
  puts("1");                /* Finish the expression */
}

Illustrated (which I trust shows the algorithm):

deleted 741 characters in body
Source Link
rici
  • 691
  • 4
  • 7
Loading
deleted 741 characters in body
Source Link
rici
  • 691
  • 4
  • 7
Loading
Source Link
rici
  • 691
  • 4
  • 7
Loading