Simple Task Solved Thrice

15

You should write 3 programs and/or functions in one language.

All of these programs should solve the same task but they all should give different (but valid) outputs. (I.e. for every pair of programs their should be some input which generates different (but valid) sets of output numbers.)

The task

  • You are given an integer n, greater than 1
  • You should return or output n distinct positive integers, and none of them should be divisible by n.
  • The order of the numbers doesn't matter and a permutation of numbers doesn't count as different outputs.

A valid triplet of programs with some input => output pairs:

program A:
    2 => 5 9
    4 => 5 6 9 10
    5 => 2 4 8 7 1

program B:
    2 => 1 11
    4 => 6 9 1 2
    5 => 4 44 444 4444 44444

program C (differs only in one number from program B):
    2 => 1 13
    4 => 6 9 1 2
    5 => 4 44 444 4444 44444

Scoring

  • Your score is the sum of the lengths of the 3 programs or functions.
  • Lower score is better.
  • If your programs/functions share code, the shared code should be counted into the length of every program that uses the code.

randomra

Posted 2015-11-26T19:00:45.497

Reputation: 19 909

1Does each program have to be able to be run from its own file without any includes, or can the programs depend on some shared module/library that is only counted once? – quintopia – 2015-11-26T19:09:24.900

@quintopia The programs/functions should not share code. If they do, the shared code should be counted into the length of all programs that use the code. – randomra – 2015-11-26T19:13:27.727

Answers

4

Pyth, 17 16 bytes

5 bytes:

^LhQQ

Outputs:

2: [1, 3]
3: [1, 4, 16]
4: [1, 5, 25, 125]

6 bytes:

mh*QdQ

Outputs:

2: [1, 3]
3: [1, 4, 7]
4: [1, 5, 9, 13]

5 bytes:

|RhQQ

Outputs:

2: [3, 1]
3: [4, 1, 2]
4: [5, 1, 2, 3]

Alternate version, in increasing order: -ShQQ

isaacg

Posted 2015-11-26T19:00:45.497

Reputation: 39 268

1Ooh. I like that third scheme. – quintopia – 2015-11-26T19:32:48.233

@isaacg oh, sorry – Maltysen – 2015-11-26T20:18:27.530

8

J, 16 bytes

Function 1, 5 bytes

p:^i.

Function 2, 6 bytes

+p:^i.

Function 3, 5 bytes

>:^i.

How it works

Function 1

p:^i.     Right argument: y

   i.     Compute (0 ... y-1).
p:        Compute P, the prime at index y (zero-indexed).
  ^       Return all powers P^e, where e belongs to (0 ... y-1).

Since P is prime and P > y, y cannot divide Pe.

Function 2

+p:^i.    Right argument: y

 p:^i.    As before.
+         Add y to all results.

If y divided Pe + y, it would also divide Pe + y - y = Pe.

Function 3

>:^i.     Right argument: y

   i.     Compute (0 ... y-1).
>:        Compute y+1.
  ^       Return all powers (y+1)^e, where e belongs to (0 ... y-1).

If y divided (y+1)e some prime factor Q of y would have to divide (y+1)e.

But then, Q would divide both y and y+1 and, therefore, y + 1 - y = 1.

Dennis

Posted 2015-11-26T19:00:45.497

Reputation: 196 637

3

Dyalog APL, 16 17 bytes

1+⊢×⍳

⊢+1+⊢×⍳

1+⊢*⍳

lirtosiast

Posted 2015-11-26T19:00:45.497

Reputation: 20 331

2

Vitsy, 54 bytes

Programs:

V1V\[DV*1+N' 'O1+]
V2V\[DV*1+N' 'O1+]
V3V\[DV*1+N' 'O1+]

Outputs:

2 => 3 7
4 => 5 9 13 17
5 => 6 11 16 21 26
2 => 5 7
4 => 9 13 17 21
5 => 11 16 21 26 31
2 => 7 9
4 => 13 17 21 25 
5 => 16 21 26 31 36

How it works (using the first program as the explanation):

V1V\[DV*1+N' 'O1+]
V                  Capture the implicit input as a final global variable.
 1                 Push one to the stack for later use.
  V\[            ] Do everything in the brackets input times.
     D             Duplicate the top item of the stack.
      V            Push the global variable to the stack.
       *1+         Multiply, then add 1. This makes it non-divisible.
          N' 'O    Output the number followed by a space.
               1+  Add one to the number left in the stack.

Try it online!

Addison Crump

Posted 2015-11-26T19:00:45.497

Reputation: 10 763

2

CJam, 8 + 8 + 8 = 24 bytes

{,:)))+}
{_,f*:)}
{)_(,f#}

These are three unnamed functions which expect n to be on the stack and leave a list of integers in its place. I'm not sure this is optimal, but I'll have to hunting for a shorter solution later.

Test suite.

Results:

{,:)))+}
2 => [1 3]
3 => [1 2 4]
4 => [1 2 3 5]
5 => [1 2 3 4 6]

{_,f*:)}
2 => [1 3]
3 => [1 4 7]
4 => [1 5 9 13]
5 => [1 6 11 16 21]

{)_(,f#}
2 => [1 3]
3 => [1 4 16]
4 => [1 5 25 125]
5 => [1 6 36 216 1296]

The first one also works as

{_),:)^}

or

{_(,+:)}

Martin Ender

Posted 2015-11-26T19:00:45.497

Reputation: 184 808

your Results show all three giving the same output when n=2 – Sparr – 2015-11-26T20:50:03.583

@Sparr That's allowed. Check the examples in the challenge. The only requirement is that they compute different functions, not different results on every input. – Martin Ender – 2015-11-26T20:51:00.417

Ahh, they have to have different results on some input. That's what I misread. – Sparr – 2015-11-26T20:51:46.577

2

Perl, 79

One char added to each program because this requires the -n flag.

for$a(0..$_-1){say$_*$a+1}
for$a(1..$_){say$_*$a+1}
for$a(2..$_+1){say$_*$a+1}

Fairly straightforward.

Doorknob

Posted 2015-11-26T19:00:45.497

Reputation: 68 138

2

Mathematica, 12 + 12 + 12 = 36 bytes

# Range@#-1&
# Range@#+1&
#^Range@#+1&

Tests:

# Range@#-1&[10]
(* -> {9, 19, 29, 39, 49, 59, 69, 79, 89, 99} *)
# Range@#+1&[10]
(* -> {11, 21, 31, 41, 51, 61, 71, 81, 91, 101} *)
#^Range@#+1&[10]
(* -> {11, 101, 1001, 10001, 100001, 1000001, 10000001, 100000001, 1000000001, 10000000001} *)

LegionMammal978

Posted 2015-11-26T19:00:45.497

Reputation: 15 731

Could you add some example outputs? – Paŭlo Ebermann – 2015-11-26T22:00:03.993

2

Python 2, 79 bytes

lambda n:range(1,n*n,n)
lambda n:range(1,2*n*n,2*n)
lambda n:range(1,3*n*n,3*n)

Three anonymous function that start at 1 and count up by each of n, 2*n, 3*n for n terms.

xnor

Posted 2015-11-26T19:00:45.497

Reputation: 115 687

1

Python 2, 125 bytes

N=input();print[i*N+1for i in range(N)]
N=input();print[i*N+1for i in range(1,N+1)]
N=input();print[i*N+1for i in range(2,N+2)]

Each line here is a complete program. The most obvious solution in my mind.

EDIT @Sherlock9 saved two bytes.

quintopia

Posted 2015-11-26T19:00:45.497

Reputation: 3 899

1

Seriously, 20 bytes

,;r*1+

,;R*1+

,;R1+*1+

Yeah, this isn't optimal...

lirtosiast

Posted 2015-11-26T19:00:45.497

Reputation: 20 331

1

Par, 16 bytes

The custom encoding, described here, uses only one byte per character.

✶″{*↑                   ## 3 => (0 1 2) => (0 3 6)  => (1 4 7)
✶″U{ⁿ↑                  ## 3 => (1 2 3) => (3 9 27) => (4 10 28)
✶U¡↑◄                   ## 3 => (1 2 3) =>             (1 2 4)

Outputs

2 => (1 3)
3 => (1 4 7)
4 => (1 5 9 13)
5 => (1 6 11 16 21)

2 => (3 5)
3 => (4 10 28)
4 => (5 17 65 257)
5 => (6 26 126 626 3126)

2 => (1 3)
3 => (1 2 4)
4 => (1 2 3 5)
5 => (1 2 3 4 6)

Ypnypn

Posted 2015-11-26T19:00:45.497

Reputation: 10 485

1

Haskell, 54 bytes

f n=n+1:[1..n-1]
g n=5*n+1:[1..n-1]
h n=9*n+1:[1..n-1]

These three functions are pretty straightforward so…

arjanen

Posted 2015-11-26T19:00:45.497

Reputation: 151

1

Octave, 11 + 13 + 13 = 37 bytes

@(a)1:a:a^2
@(a)a-1:a:a^2
@(a)(1:a)*a+1

alephalpha

Posted 2015-11-26T19:00:45.497

Reputation: 23 988

1

Haskell, 50

f n=n+1:[1..n-1]
f n=1:[n+1..2*n-1]
f n=[1,n+1..n^2]

Examples:

 f1 5=[6,1,2,3,4]
 f2 5=[1,6,7,8,9]
 f3 5=[1,6,11,16,21]

proud haskeller

Posted 2015-11-26T19:00:45.497

Reputation: 5 866

0

TI-Basic (TI-84 Plus CE), 55 40 bytes total

PRGM:C 12 bytes
    seq(AnsX+1,X,1,Ans
PRGM:B 14 bytes
    seq(AnsX+1,X,2,Ans+1
PRGM:C 14 bytes
    seq(AnsX+1,X,3,Ans+2

Simple, similar to many other answers here, each displays a list of the numbers (X+A)N+1 for X in range(N) and with A being which program (1, 2, or 3).

Old solution (55 bytes):

PRGM:C 17 bytes
    Prompt N
    For(X,1,N
    Disp XN+1
    End
PRGM:B 19 bytes
    Prompt N
    For(X,2,N+1
    Disp XN+1
    End
PRGM:C 19 bytes
    Prompt N
    For(X,3,N+2
    Disp XN+1
    End

Simple, similar to many other answers here, each displays the numbers (X+A)N+1 for X in range(N) and with A being which program (1, 2, or 3).

pizzapants184

Posted 2015-11-26T19:00:45.497

Reputation: 3 174

0

Golfscript, 50 51 57 bytes

A Golfscript version of what used to be quintopia's Python code. Each function takes n off the stack.

{.,{1$*)}%\;}:f;    i*n+1 for i in range(n)
{.,{)1$*)}%\;}:g;   i*n+1 for i in range(1,n+1)
{.,{1$)\?}%\;}:h;   (n+1)**i for i in range(n)

Sherlock9

Posted 2015-11-26T19:00:45.497

Reputation: 11 664