15
\$\begingroup\$

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.
\$\endgroup\$
  • 1
    \$\begingroup\$ Does 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? \$\endgroup\$ – quintopia Nov 26 '15 at 19:09
  • \$\begingroup\$ @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. \$\endgroup\$ – randomra Nov 26 '15 at 19:13

16 Answers 16

4
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Ooh. I like that third scheme. \$\endgroup\$ – quintopia Nov 26 '15 at 19:32
  • \$\begingroup\$ @isaacg oh, sorry \$\endgroup\$ – Maltysen Nov 26 '15 at 20:18
8
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Dyalog APL, 16 17 bytes

1+⊢×⍳

⊢+1+⊢×⍳

1+⊢*⍳
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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} *)
\$\endgroup\$
  • \$\begingroup\$ Could you add some example outputs? \$\endgroup\$ – Paŭlo Ebermann Nov 26 '15 at 22:00
2
\$\begingroup\$

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

{_(,+:)}
\$\endgroup\$
  • \$\begingroup\$ your Results show all three giving the same output when n=2 \$\endgroup\$ – Sparr Nov 26 '15 at 20:50
  • \$\begingroup\$ @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. \$\endgroup\$ – Martin Ender Nov 26 '15 at 20:51
  • \$\begingroup\$ Ahh, they have to have different results on some input. That's what I misread. \$\endgroup\$ – Sparr Nov 26 '15 at 20:51
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Seriously, 20 bytes

,;r*1+

,;R*1+

,;R1+*1+

Yeah, this isn't optimal...

\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

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…

\$\endgroup\$
1
\$\begingroup\$

Octave, 11 + 13 + 13 = 37 bytes

@(a)1:a:a^2
@(a)a-1:a:a^2
@(a)(1:a)*a+1
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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]
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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