6
\$\begingroup\$

Challenge

Your submission should consist of 2 or more programs each written in distinct languages. They can be arbitrarily numbered from 1 to k.

Each program p, given a natural number n should print the next n prime numbers (initially, the next prime is the 0-th prime, 2) on a single line, followed by a single blank newline, followed by a program fragment, which, when appended to the p+1 mod kth program, produces the program which does precisely the same thing, but with the next prime being the n+1th prime number.

Details

  • Your programs may accept its input as a function argument, or from standard input, or the closest equivalent in your languages.
  • Your programs may not read data from any external source other than to read n from standard input. If this data is required by your program, it must be included in the source code fragment generated by the previous program.
  • Your programs may not compute any prime numbers less than the next prime number.
  • The prime numbers may be separated by any delimiter other than a newline.
  • You may not use any language primitives or libraries for prime number generation - you must write the algorithm yourself.
  • You may use standard libraries for things other than prime generation.
  • Two languages which differ only in their versions are not said to be distinct languages.
  • Two languages whose semantics largely overlap (for example, C and C++) are not said to be distinct.
  • Your programs may do whatever they like for undefined inputs.
  • Your programs are required to support generating prime numbers up to 2^32-1. You may assume whatever you need about the runtime environment to ensure this is the case (i.e. you can assume your C program will be run on a machine where int has at least 32 bits). If this assumptions is non-trivial it should be stated explicitly.
  • If running your program involves a non-trivial compilation step it should be stated how to do it, so others can reproduce it.

Input/Output

next    in    out
0       10    2,3,5,7,11,13,17,19,23,29
25      13    101,103,107,109,113,127,131,137,139,149,151,157,163
7       4     19,23,29,31

Scoring

Because disk space is very expensive, your programs should be as small as possible. Because more is obviously better, your submission should consist of as many programs as possible. The score of your submission is the average byte count of your programs divided by the number of distinct languages used. Lowest score wins.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Java + Python 2, Score = (302 + 218) / 2 / 2 = 130

Here is a brief explanation on how this works:

  • Choose a program and pick a positive integer, n; for Java, pass n as a command line argument; for Python, pass n through stdin.
    • The program will then print a list of n primes followed by 2 newlines and then a snippet.
  • Append this to a fresh copy of the program which was not chosen.
  • Choose a new n and pass it to this modified program
    • The program will then print a list of n primes followed by 2 newlines and then a snippet.
  • Append this to a fresh copy of the program which was originally chosen.
  • Rinse, repeat.

Golfed

Java, 302 bytes

Netbeans is able to compile this even with the A class missing, I haven't been able to figure out how. But once I do, I will add the flags to the byte count.

public class P{static int i=2;public static void main(String[]a){int l=2,n=Integer.parseInt(a[0]),x;String s="";try{new A().a();}catch(Exception e){}while(n>0){if(i>1){boolean b=false;for(x=2;x<i;x++){if(i%x==0 && i!=2)b=true;}if(!b){s+=s==""?i:","+i;l=i;n--;}i++;}}System.out.print(s+"\n\n+"+(l-1));}}

Python 2, 218 bytes

n=int(input())
s=[]
i=-1
while n:
 if i>1:
  for x in range(2,i):
   if i%x==0and i!=2:break
  else:s+=[i];n-=1
  i+=1
 if n==0:print",".join(str(y)for y in s)+"\n\nclass A{void a(){P.i="+str(s[-1]+1)+";}}"
 if i<0:i=2

Un-Golfed

Java

public class PPCG {

   public static int i = 2;

   public static void main(String[] args){
       int n = Integer.parseInt(args[0]);
       int last = 2;
       String s = "";
       try{
           new A().a();
       }catch(Exception e){}
       while(n>0){
           if(i>1){
               boolean broken=false;
               for(int x=2;x<i;x++){
                   if(i%x==0 && i!=2){
                       broken=true;
                   }
               }
               if(!broken){
                   s += s==""?i:","+i;
                   last = i;
                   n-=1;
               }
               i+=1;
           }
       }
       System.out.println(s+"\n\n+"+(last-1));
   }
}

Python 2

n=int(input())
s=[]
i=-1
while n:
  if i>1:
    for x in range(2,i):
      if i%x==0 and i!=2:
        break
    else:
      s+=[i]
      n -= 1
    i+=1
  if n==0:
    print ",".join(str(y) for y in s)+"\n\nclass A{public static void a(){PPCG.i="+str(s[-1]-1)+";}}"
  if i<0:
    i = 2

Update

  • -17 [16-12-13] Misc golfing
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.