73
\$\begingroup\$

According to this XKCD comic, there is a formula to determine whether or not the age gap in a relationship is "creepy". This formula is defined as:

(Age/2) + 7

being the minimum age of people you can date.

Therefore a relationship is creepy if either of the people in said relationship are younger than the minimum age of the other.

Given the age of two people, can you output whether or not that relationship is creepy?

Rules

  1. Your program should take two integers as input, the age of both people in the relationship. These can be taken in any reasonable format.

  2. Your program must then output a truthy or falsy value describiing whether or not the relationship is "creepy" (Truthy = Creepy).

  3. Standard loopholes are not allowed.
  4. This puzzle is Code Golf, so the answer with the shortest source code in bytes wins

Test Cases

40, 40    - Not Creepy
18, 21    - Not Creepy
80, 32    - Creepy
15, 50    - Creepy
47, 10000 - Creepy
37, 38    - Not Creepy
22, 18    - Not Creepy
\$\endgroup\$
  • 3
    \$\begingroup\$ How should age/2 be rounded? Probably up if the exact half is supposed to be the minimum? 17,21 would make a good test case. \$\endgroup\$ – Martin Ender May 24 '17 at 18:20
  • 4
    \$\begingroup\$ @MartinEnder The value is a minimum, so do not round at all. It doesn't have to be an integer. \$\endgroup\$ – Leo May 24 '17 at 18:24
  • 81
    \$\begingroup\$ You could also add 13, 13 - Creepy. \$\endgroup\$ – Greg Martin May 24 '17 at 19:20
  • 12
    \$\begingroup\$ 47, 10000 is an... interesting combination. I would also like to point out that according to this formula, it is creepy for Doctor Who to date any human. \$\endgroup\$ – David Conrad May 26 '17 at 15:01
  • 8
    \$\begingroup\$ @DavidConrad - well yeah. its basically beastiality on his part.... \$\endgroup\$ – Batman May 26 '17 at 16:47

63 Answers 63

6
\$\begingroup\$

Jelly, 6 bytes

H+7>ṚṀ

Try it online!

Seemingly different algorithm than Comrade's.

\$\endgroup\$
  • \$\begingroup\$ Well, its the same now... ;p \$\endgroup\$ – Draco18s May 24 '17 at 21:47
  • \$\begingroup\$ @Draco18s It was always the same with the earlier 05AB1E submission though. \$\endgroup\$ – Erik the Outgolfer May 25 '17 at 11:31
17
\$\begingroup\$

Python 3, 26 bytes

lambda x:max(x)/2+7>min(x)

Try it online!
The input is a list with both ages

\$\endgroup\$
  • \$\begingroup\$ Had the same before I read existing answers. +1 \$\endgroup\$ – ElPedro May 24 '17 at 20:08
  • \$\begingroup\$ It appears you could actually assume a tuple where it's always (younger,older) - just asked OP - wonder what he'll say. \$\endgroup\$ – rm-vanda May 25 '17 at 16:07
  • \$\begingroup\$ @rm-vanda I asked earlier, you can't assume \$\endgroup\$ – Stephen May 26 '17 at 17:28
15
\$\begingroup\$

05AB1E, 8 6 bytes

;7+R‹Z

Try it online! or Try all test

         # Implicit Input: an array of the ages
;        # Divide both ages by 2
 7+      # Add 7 to both ages
   R     # Reverse the order of the ages
         #    this makes the "minimum age" line up with the partner's actual age
    ‹    # Check for less than the input (vectorized)
     Z   # Push largest value in the list
\$\endgroup\$
  • 3
    \$\begingroup\$ As a C programmer I agree that 2 is truthy. \$\endgroup\$ – gmatht May 26 '17 at 12:44
  • \$\begingroup\$ @gmatht This should always return either 0 or 1, never 2. \$\endgroup\$ – Riley May 26 '17 at 14:07
  • 4
    \$\begingroup\$ [13,13] is doubly creepy. \$\endgroup\$ – gmatht May 27 '17 at 3:30
  • 1
    \$\begingroup\$ @gmatht I guess that would be double. I didn't think about numbers that small. It's still truthy though. \$\endgroup\$ – Riley May 27 '17 at 4:13
  • \$\begingroup\$ @Riley 2 is not truthy, see this. \$\endgroup\$ – Okx Jun 2 '17 at 17:40
13
\$\begingroup\$

NAND gates, 551

16-bit creepiness calculator Created with Logisim

Same principle as my other answer, but takes 2-byte signed inputs, so it can handle 47, 10000. Works for ALL test cases!

This is not optimal for the given test cases, as 10000 can be expressed with only 15 of the 16 bits, but it works for any ages in the range [-32768, 32768). Note that any negative age will return 1.

Inputs on left (no particular order, 1-bit on top). Output in lower right.

\$\endgroup\$
10
\$\begingroup\$

NAND gates, 274 262

Original:

Better: Created with Logisim

This takes two inputs on the left as 1-byte signed integers, with the 1-bit at the top. Output is in the lower left; the truthy and falsey here should be obvious.

Works for all test cases except 47, 10000, so I guess this is technically not a valid answer. However, the oldest person on (reliable) record was 122, so 8 bits (max 127) will work for any scenario ever possible to this point. I'll post a new answer (or should I edit this one?) when I finish the 16-bit version.

16-bit version is done!

You'll notice some vertical sections of the circuit. The first one (from the left) determines which input is greater. The next two are multiplexers, sorting the inputs. I then add 11111001 (-7) to the lesser in the fourth section, and I conclude by comparing twice this to the greater input. If it is less, the relationship is creepy. Since I shift the bits to double, I must take into account the unused bit of lesser-7. If this is a 1, then lesser-7 is negative, and the younger of the two is no older than six. Creepy. I finish with an OR gate, so if either creepiness test returns 1, the entire circuit does.

If you look closely, you'll see that I used seven one constants (hardcoding the 11111011 and the trailing 0). I did this because Logisim requires at least one value going in for a logic gate to produce an output. However, each time a constant is used, two NAND gates ensure a 1 value regardless of the constant.

-12 gates thanks to me!

\$\endgroup\$
  • \$\begingroup\$ Noticed an obvious optimization. If you point it out before I edit, I'll still credit you! \$\endgroup\$ – Khuldraeseth na'Barya Sep 4 '17 at 0:02
9
\$\begingroup\$

C#, 22 bytes

n=>m=>n<m/2+7|m<n/2+7;
\$\endgroup\$
  • 1
    \$\begingroup\$ I'm not much of a C# programmer, but is the final semicolon required as part of the function? \$\endgroup\$ – Olivier Grégoire May 27 '17 at 19:02
  • 1
    \$\begingroup\$ @OlivierGrégoire It's just invalid syntax if it's omitted; this is an anonymous function \$\endgroup\$ – cat May 27 '17 at 22:00
8
\$\begingroup\$

C, 29 bytes

#define f(a,b)a/2+7>b|b/2+7>a

How it works:

  • #define f(a,b) defines a macro function f that takes two untyped arguments.
  • a/2+7>b checks if the first age divided by two plus seven is larger than the second age.
  • b/2+7>a checks if the second age divided by two plus seven is larger than the first age.
  • If either of the above values are true, return 1 (creepy). Otherwise, return 0 (not creepy).

Try it online!

\$\endgroup\$
  • \$\begingroup\$ you should flip the comparison, it should be like >b not <b \$\endgroup\$ – Khaled.K May 24 '17 at 20:26
  • \$\begingroup\$ Quote "being the minimum age" means you have to check if age >= min, also you need AND instead of OR, since both sides has to satisfy in order for it to not be creepy, test case "47, 10000 - Creepy" \$\endgroup\$ – Khaled.K May 24 '17 at 20:40
  • \$\begingroup\$ okay I've fixed the error, but the logic is wrong, it return true, tio.run \$\endgroup\$ – Khaled.K May 24 '17 at 20:42
  • 1
    \$\begingroup\$ @Tas nope. \$\endgroup\$ – MD XF May 24 '17 at 23:35
  • 1
    \$\begingroup\$ Cheers, thanks for the link \$\endgroup\$ – Tas May 24 '17 at 23:39
7
\$\begingroup\$

JavaScript (ES6), 21 bytes

a=>b=>a<b/2+7|b<a/2+7

Returns 0 for not creepy, 1 for creepy.

f=a=>b=>a<b/2+7|b<a/2+7

console.log(f(40)(40));
console.log(f(18)(21));
console.log(f(80)(32));
console.log(f(15)(50));
console.log(f(47)(10000));
console.log(f(37)(38));
console.log(f(22)(18));

\$\endgroup\$
  • \$\begingroup\$ Save a byte with currying: a=>b=> instead of (a,b)=>, call with f(40)(40). \$\endgroup\$ – Stephen May 24 '17 at 19:30
  • \$\begingroup\$ @StephenS, thank you, learned something new! \$\endgroup\$ – Rick Hitchcock May 24 '17 at 19:32
  • \$\begingroup\$ No problem, I learned it when someone told me the same thing :) it only works with two parameters though, after that it's not worth it. \$\endgroup\$ – Stephen May 24 '17 at 19:33
7
\$\begingroup\$

R, 26 25 bytes

-1 byte thanks to @djhurio

any(rev(a<-scan())<a/2+7)

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Retina, 20 bytes

O`.+
^1{7}(1+)¶1\1\1

Try it online!

Input is in unary with a linefeed between the two numbers. Output is 0 (not creepy) or 1 (creepy).

Explanation

O`.+

Sort the two numbers, so we know that the larger one is second.

^1{7}(1+)¶1\1\1

Call the smaller age a and the larger age b. We first capture a-7 in group 1. Then we try to match 2*(a-7)+1 in b, which means b >= 2*(a-7)+1 or b >= 2*(a-7) or b/2+7 > a which is the criterion for a creepy relationship.

\$\endgroup\$
5
\$\begingroup\$

TI-Basic, 20 10 9 bytes

max(2Ans<14+max(Ans

-10 bytes by using a list and part of Timtech's suggestion

-1 byte using lirtosiast's suggestion

Takes in a list of two ages, "{40,42}:prgmNAME"

Returns 1 for 'creepy' and 0 for 'not creepy'.

\$\endgroup\$
  • \$\begingroup\$ Does TI-BASIC automatically close parentheses at a testing-symbol (< <= = != >= >)? \$\endgroup\$ – Zacharý Sep 4 '17 at 16:03
  • \$\begingroup\$ @Zacharý No, TI-Basic only closes parentheses at the end of a line or a colon. \$\endgroup\$ – pizzapants184 Sep 4 '17 at 16:16
  • \$\begingroup\$ Oh, whoops I forgot that the input was being taken as a list of numbers! \$\endgroup\$ – Zacharý Sep 4 '17 at 16:21
4
\$\begingroup\$

GNU APL 1.2, 23 bytes

Defines a function that takes two arguments and prints 1 if creepy, 0 if not.

∇A f B
(A⌊B)<7+.5×A⌈B
∇

Explanation

begins and ends the function
A f B is the function header; function is named f and takes two arguments, A and B (functions in APL can be monadic - taking one argument - or dyadic - taking two arguments)
A⌊B is min(A,B) and A⌈B is max(A,B)
APL is evaluated right-to-left, so parentheses are needed to ensure proper precedence

The other operators are self explanatory.

Code might be golf-able, I'm still new to code-golf.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site! \$\endgroup\$ – OldBunny2800 May 25 '17 at 12:22
  • \$\begingroup\$ Wow, nice, GNU APL, haven't seen that in a while. \$\endgroup\$ – Zacharý Sep 4 '17 at 14:10
  • \$\begingroup\$ Also, it's possible to take the arguments as a list: f X then (⌊/X)<7+.5×⌈/X. IIRC you can remove the newline between the second and third lines. \$\endgroup\$ – Zacharý Sep 4 '17 at 14:11
  • \$\begingroup\$ @Zacharý Yes, anonymous lambdas are possible. They are not supported by this version of GNU APL and the newer one does not compile on Mac. Some of my other answers use APL 1.7 because I test them on Ubuntu. I have not used lambdas (might fix them later) because I'm still pretty new to APL. \$\endgroup\$ – Arc676 Sep 4 '17 at 14:14
  • \$\begingroup\$ Try out ngn-apl. It's open-source like GNU APL, but in all honesty it's better. \$\endgroup\$ – Zacharý Sep 4 '17 at 14:20
4
\$\begingroup\$

Python 3, 74 45 bytes

First Code Golf, probably terrible.

29 byte reduction by @Phoenix

lambda a,b:0 if(a/2)+7>b or(b/2)+7>a else 1
\$\endgroup\$
  • \$\begingroup\$ Hi, please be sure to properly format your code using the markdown system. \$\endgroup\$ – Leo May 24 '17 at 18:47
  • \$\begingroup\$ don't worry, someone beat me to it, ill fix it though \$\endgroup\$ – KuanHulio May 24 '17 at 18:47
  • \$\begingroup\$ You can get rid of some of the spaces there :) \$\endgroup\$ – Beta Decay May 24 '17 at 18:47
  • \$\begingroup\$ Also, lambda a,b:0 if(a/2)+7>b or(b/2)+7>a else 1 should work and is shorter by a lot. \$\endgroup\$ – Pavel May 24 '17 at 18:55
  • 1
    \$\begingroup\$ lambda a,b:a/2+7>b or b/2+7>a. Abandon the burden of those pesky 1s and 0s and embrace the power of True/False! \$\endgroup\$ – Value Ink May 31 '17 at 1:31
3
\$\begingroup\$

JavaScript (ES6), 27 bytes

f=a=>b=>b>a?f(b)(a):b>a/2+7

No currying (call like f(a,b) instead of f(a)(b))

f=(a,b)=>b>a?f(b,a):b>a/2+7

If b > a, swap parameters and retry. Otherwise, check. Currying doesn't save any bytes because of the recursive call.

f=a=>b=>b>a?f(b)(a):b>a/2+7

console.log(f(18)(22))
console.log(f(22)(18))
console.log(f(18)(21))

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

Java, 21 bytes

a->b->a/2+7>b|b/2+7>a

Absolutely not original.

Testing

Try it online!

import java.util.function.*;

public class Pcg122520 {
  static IntFunction<IntPredicate> f = a->b->a/2+7>b|b/2+7>a;
  public static void main(String[] args) {
    int[][] tests = {
      {40, 40},
      {18, 21},
      {80, 32},
      {15, 50},
      {47, 10000},
      {37, 38},
      {22, 18}
    };
    for (int[] test: tests) {
      System.out.printf("%d, %d - %s%n", test[0], test[1], f.apply(test[0]).test(test[1]) ? "Creepy" : "Not creepy");
    }
  }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ +1 for mentioning "Absolutely not original.". Almost no one else does, while they are almost all the same. And I took the liberty of adding a TIO-link from your test code. Not sure why you always add a test code, but not a TIO-link?.. :) \$\endgroup\$ – Kevin Cruijssen May 26 '17 at 10:22
  • 1
    \$\begingroup\$ Thanks Kevin! I sometimes do add one, sometimes not. It depends on whether I have my IDE open or closed. It's as simple as that! :P Also, I show the test code in order for people to understand what makes this lambda valid. :) \$\endgroup\$ – Olivier Grégoire May 26 '17 at 10:40
3
\$\begingroup\$

Python 3, 31 bytes

lambda a,b:abs(a-b)>min(a,b)-14

Not much shorter than the other python submissions, but I found a slightly different way to check for creepiness. I noticed that the acceptable difference between ages is equal to min - 14. This follows from algebraically rearranging the formula.

min = (max/2) + 7
min - 7 = max/2
2*min - 14 = max

dif = max - min
max = dif + min

2*min - 14 = dif + min
min - 14 = dif

This let me solve without needing two constants, and also without needing to use both max and min, instead using abs(a-b). From a golfing perspective I only got one byte less than @nocturama's solution, but I used a slightly different formula to do it.

\$\endgroup\$
  • \$\begingroup\$ Surely this fails on [37,53] (not in the test suite but) within the (x/2)+7 spirit of this fairway \$\endgroup\$ – Alexx Roche Oct 15 '17 at 12:32
  • \$\begingroup\$ @AlexxRoche Nope, when given [37,53] as [a,b], the calculation should become abs(37 - 53) > min(37, 53) - 14 = abs(-16) > 37 - 14 = 16 > 23 = False This is the correct answer, because according to (x/2) + 7, the minimum age for 53 is 53/2 + 7 = 26.5 + 7 = 33.5 \$\endgroup\$ – Delya Erricson Oct 17 '17 at 3:56
3
\$\begingroup\$

Excel, 26 24 Bytes

Cell formula that takes input as numbers from cell range A1:B1 and output a boolean value representing creepiness to the formula cell

=OR(A1/2+7>B1,B1/2+7>A1)

Old Version, 26 Bytes

=MAX(A1:B1)/2+7>MIN(A1:B1)
\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 10 9 10 bytes

2min(Ans)-14≤max(Ans

List input from Ans, outputs 1 if "creepy" or 0 otherwise.

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

Mathics, 16 bytes

Max@#/2+7<Min@#&

Try it online!

-2 bytes thanks to @GregMartin

True for not creepy, false for creepy.

            (* Argument: A list of integers     *)
Max@#       (* The maximum of the input         *)
 /2+7       (* Divided by 2, plus 7             *)
  <         (* Is less than?                    *)
   Min@#    (* The minimum of the input         *)
    &       (* Anonymous function               *)
\$\endgroup\$
  • \$\begingroup\$ Save 2 bytes by taking the ages as a list: Max@#/2+7<Min@#& \$\endgroup\$ – ngenisis May 24 '17 at 19:28
2
\$\begingroup\$

SAS, 77 bytes

%macro t(a,b);%put%eval(%sysfunc(max(&a,&b))/2+7>%sysfunc(min(&a,&b)));%mend;
\$\endgroup\$
2
\$\begingroup\$

Röda, 16 bytes

{sort|[_<_/2+7]}

Try it online!

This is an anonymous function that takes input as two literals (not an array) from the input stream.

Explanation

{sort|[_<_/2+7]}                 Anonymous function
 sort                            Sorts the numbers in the input stream
     |[       ]                  And push
       _<                        whether the smaller value  is less than
         _/2+7                   the greater value / 2 + 7
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 15 bytes

{.max/2+7>.min}

Try it

Expanded

{ # bare block lambda with implicit parameter 「$_」
  # (input is a List)

  .max / 2 + 7
  >
  .min
}
\$\endgroup\$
2
\$\begingroup\$

Crystal, 44 27 bytes

-17 from looking at daniero's answer in Ruby.

def a(a)7+a.max/2>a.min end

Try it online!

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

Python 3 - 32 27 bytes

Unable to comment, but I got a slightly shorter answer than the other Python 3 solution:

lambda *a:min(a)<max(a)/2+7

-5 thanks to @Cyoce!

\$\endgroup\$
  • \$\begingroup\$ you can remove the space in lambda *a \$\endgroup\$ – Cyoce Nov 30 '17 at 23:37
1
\$\begingroup\$

Fourier, 37 bytes

oI~AI~B>A{1}{A~SA~BS~B}A/2+7>B{1}{@o}

Try it on FourIDE!

Takes two numbers as input. Will golf later.

\$\endgroup\$
  • \$\begingroup\$ It doesn't need to output a string like that, can be truthy or falsy \$\endgroup\$ – Leo May 24 '17 at 18:26
  • \$\begingroup\$ Nice golf. That was quick. \$\endgroup\$ – MD XF May 24 '17 at 18:38
1
\$\begingroup\$

PHP, 29 Bytes

prints 1 for creepy, nothing for Not creepy

<?=max($_GET)/2+7>min($_GET);

Try it online!

\$\endgroup\$
  • \$\begingroup\$ As I understand the rules this fails on [34,19] \$\endgroup\$ – Alexx Roche Oct 15 '17 at 12:43
1
\$\begingroup\$

Japt, 11 bytes

Returns true for "creepy" and false for not.

wV /2+7>UmV

Try it online


Explanation

      :Implicit input of first integer U
wV    :Get the maximum of U and the second integer V
/2+7  :Divide by 2 & add 7
>     :Check if the result is greater than...
UmV   :the minimum of the two integers.
\$\endgroup\$
1
\$\begingroup\$

J, 10 bytes

<.<7+2%~>.

Outputs 1 for not creepy, 0 for creepy

Explanation

<.          NB. the minimum
  >         NB. is greater than
    7+2%~>. NB. half the maximum + 7
\$\endgroup\$
1
\$\begingroup\$

J-uby, 25 bytes

:>%[:min,:max|~:/&2|:+&7]

Call like f^[80,32]. Gives true for not creepy, false for creepy.

Explanation

    :min                  # the minimum
:>%[    ,               ] # is greater than
         :max|            # the maximum...
              ~:/&2|        # over two...
                    :+&7    # plus 7 
\$\endgroup\$
  • \$\begingroup\$ This is a beautiful language. I spent a long time trying to accomplish similar ends with Ruby (I called it "Blurb"), but leaning heavily on method_missing led to too much complexity. This approach is clean and elegant. Congrats! \$\endgroup\$ – Jordan May 25 '17 at 2:53
  • \$\begingroup\$ @Jordan thanks! I can't take all the credit, as this was heavily inspired by J (hence the name). I'm open to suggestions from a fellow Ruby programmer if you have any. \$\endgroup\$ – Cyoce May 25 '17 at 3:04
  • \$\begingroup\$ @Jordan you should see some of the other more complex J-uby answers. They're quite something. \$\endgroup\$ – Cyoce May 25 '17 at 3:21
1
\$\begingroup\$

AWK, 26 bytes

{$0=$1/2+7>$2||$2/2+7>$1}1

Try it online!

Outputs 1 for "Creepy" and 0 for "Not Creepy". Could save 3 bytes if no-output could be consider a falsy value, via:

$0=$1/2+7>$2||$2/2+7>$1
\$\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.