Skip to main content
added 239 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

Primality-testing function

The following 28-byte function returns true for prime numbers and false for non-primes:

f=(n,x=n)=>n%--x?f(n,x):x==1

This can easily be modified to calculate other things. For example, this 4139-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n?n%--x?f(n,x):(x==1)+f!--x+f(n-1):0

If you already have a variable n that you want to check for primality, the primality function can be simplified quite a bit:

(f=x=>n%--x?f(x):x==1)(n)

How it works

f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n % --x ?   //   If n is not divisible by x - 1,
  f(n, x)     //     return the result of f(n, x - 1).
              //   This loops down through all numbers between n and 0,
              //     stopping when it finds a number that divides n.
  : x == 1    //   Return x == 1; for primes only, 1 is the smallest number
              //     less than n that divides n.
              //   For 1, x == 0; for 0, x == -1.

Note: This will fail with a "too much recursion" error when called with a sufficiently large input, such as 12345. You can get around this with a loop:

f=n=>eval('for(x=n;n%--x;);x==1')

Primality-testing function

The following 28-byte function returns true for prime numbers and false for non-primes:

f=(n,x=n)=>n%--x?f(n,x):x==1

This can easily be modified to calculate other things. For example, this 41-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n?n%--x?f(n,x):(x==1)+f(n-1):0

How it works

f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n % --x ?   //   If n is not divisible by x - 1,
  f(n, x)     //     return the result of f(n, x - 1).
              //   This loops down through all numbers between n and 0,
              //     stopping when it finds a number that divides n.
  : x == 1    //   Return x == 1; for primes only, 1 is the smallest number
              //     less than n that divides n.
              //   For 1, x == 0; for 0, x == -1.

Note: This will fail with a "too much recursion" error when called with a sufficiently large input, such as 12345.

Primality-testing function

The following 28-byte function returns true for prime numbers and false for non-primes:

f=(n,x=n)=>n%--x?f(n,x):x==1

This can easily be modified to calculate other things. For example, this 39-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n?n%--x?f(n,x):!--x+f(n-1):0

If you already have a variable n that you want to check for primality, the primality function can be simplified quite a bit:

(f=x=>n%--x?f(x):x==1)(n)

How it works

f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n % --x ?   //   If n is not divisible by x - 1,
  f(n, x)     //     return the result of f(n, x - 1).
              //   This loops down through all numbers between n and 0,
              //     stopping when it finds a number that divides n.
  : x == 1    //   Return x == 1; for primes only, 1 is the smallest number
              //     less than n that divides n.
              //   For 1, x == 0; for 0, x == -1.

Note: This will fail with a "too much recursion" error when called with a sufficiently large input, such as 12345. You can get around this with a loop:

f=n=>eval('for(x=n;n%--x;);x==1')
golfed 7 bytes
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

The following 3528-byte function returns true for prime numbers and 0false for non-primes:

f=(n,x=n)=>n>1?=>n%--x<2||n%x&&fx?f(n,x):0x==1

This can easily be modified to calculate other things. For example, this 4241-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n>1=>n?n%--x?f(n,x):(x<2x==1)+f(n-1):0
f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n >% 1--x ?     //   If n is greaternot thandivisible by x - 1,
  --f(n, x) < 2 ||  //     ifreturn the result of f(n, x - 1).
 is less than 2, return true; otherwise,
  n % x &&    //   This loops ifdown nthrough isall divisiblenumbers bybetween xn -and 10, 
 0; otherwise,
  f(n, x)     //    // the result of f(n, xstopping -when 1).
it finds a number that divides n.
  : x == 1    //   ThisReturn loopsx down== through1; allfor numbersprimes betweenonly, n1 andis 1,the smallest number
              //     returning 0less ifthan n is divisible by anythat ofdivides themn.
  : 0         //  // Otherwise (for allFor inputs1, lessx than== 2),0; returnfor 0.

You can golf this by 2 bytes if the input is guaranteed to be non-negative, and NaN can be returned for 1:

f=(n,x=n)=>n>1&--x<2||n%x&&f(n,x)

You can golf off 4 more bytes if the input is guaranteed to be greater than 1:

f=(n,x=n)=>--x<2||n%x&&f(n, x)

And one final byte if the output for primes can be any positive number:

f=(n,x=n)=>- == -x<2||n%x*f(n,x)1.

A grand total of 28 bytes!

Note: All of theseThis will fail with a "too much recursion" error when presentedcalled with a sufficiently large input, such as 12345.

The following 35-byte function returns true for prime numbers and 0 for non-primes:

f=(n,x=n)=>n>1?--x<2||n%x&&f(n,x):0

This can easily be modified to calculate other things. For example, this 42-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n>1?n%--x?f(n,x):(x<2)+f(n-1):0
f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n > 1 ?     //   If n is greater than 1,
  --x < 2 ||  //     if x - 1 is less than 2, return true; otherwise,
  n % x &&    //     if n is divisible by x - 1, 0; otherwise,
  f(n, x)     //     the result of f(n, x - 1).
              //   This loops down through all numbers between n and 1,
              //     returning 0 if n is divisible by any of them.
  : 0         //   Otherwise (for all inputs less than 2), return 0.

You can golf this by 2 bytes if the input is guaranteed to be non-negative, and NaN can be returned for 1:

f=(n,x=n)=>n>1&--x<2||n%x&&f(n,x)

You can golf off 4 more bytes if the input is guaranteed to be greater than 1:

f=(n,x=n)=>--x<2||n%x&&f(n,x)

And one final byte if the output for primes can be any positive number:

f=(n,x=n)=>--x<2||n%x*f(n,x)

A grand total of 28 bytes!

Note: All of these fail when presented with a sufficiently large input, such as 12345.

The following 28-byte function returns true for prime numbers and false for non-primes:

f=(n,x=n)=>n%--x?f(n,x):x==1

This can easily be modified to calculate other things. For example, this 41-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n?n%--x?f(n,x):(x==1)+f(n-1):0
f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n % --x ?   //   If n is not divisible by x - 1,
  f(n, x)     //     return the result of f(n, x - 1).
              //   This loops down through all numbers between n and 0, 
              //     stopping when it finds a number that divides n.
  : x == 1    //   Return x == 1; for primes only, 1 is the smallest number
              //     less than n that divides n.
              //   For 1, x == 0; for 0, x == -1.

Note: This will fail with a "too much recursion" error when called with a sufficiently large input, such as 12345.

yup
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

Primality-testing function

The following 35-byte function returns true for prime numbers and 0 for non-primes:

f=(n,x=n)=>n>1?--x<2||n%x&&f(n,x):0

This can easily be modified to calculate other things. For example, this 42-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n>1?n%--x?f(n,x):(x<2)+f(n-1):0

How it works

f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n > 1 ?     //   If n is greater than 1,
  --x < 2 ||  //     if x - 1 is less than 2, return true; otherwise,
  n % x &&    //     if n is divisible by x - 1, 0; otherwise,
  f(n, x)     //     the result of f(n, x - 1).
              //   This loops down through all numbers between n and 1,
              //     returning 0 if n is divisible by any of them.
  : 0         //   Otherwise (for all inputs less than 2), return 0.

You can golf this by 2 bytes if the input is guaranteed to be non-negative, and NaN can be returned for 1:

f=(n,x=n)=>n>1&--x<2||n%x&&f(n,x)

You can golf off 4 more bytes if the input is guaranteed to be greater than 1:

f=(n,x=n)=>--x<2||n%x&&f(n,x)

And one final byte if the output for primes can be any positive number:

f=(n,x=n)=>--x<2||n%x*f(n,x)

A grand total of 28 bytes!

Note: All of these fail when presented with a sufficiently large input, such as 12345.

Primality-testing function

The following 35-byte function returns true for prime numbers and 0 for non-primes:

f=(n,x=n)=>n>1?--x<2||n%x&&f(n,x):0

This can easily be modified to calculate other things. For example, this 42-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n>1?n%--x?f(n,x):(x<2)+f(n-1):0

How it works

f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n > 1 ?     //   If n is greater than 1,
  --x < 2 ||  //     if x - 1 is less than 2, return true; otherwise,
  n % x &&    //     if n is divisible by x - 1, 0; otherwise,
  f(n, x)     //     the result of f(n, x - 1).
              //   This loops down through all numbers between n and 1,
              //     returning 0 if n is divisible by any of them.
  : 0         //   Otherwise (for all inputs less than 2), return 0.

You can golf this by 2 bytes if the input is guaranteed to be non-negative, and NaN can be returned for 1:

f=(n,x=n)=>n>1&--x<2||n%x&&f(n,x)

You can golf off 4 more bytes if the input is guaranteed to be greater than 1:

f=(n,x=n)=>--x<2||n%x&&f(n,x)

And one final byte if the output for primes can be any positive number:

f=(n,x=n)=>--x<2||n%x*f(n,x)

A grand total of 28 bytes!

Primality-testing function

The following 35-byte function returns true for prime numbers and 0 for non-primes:

f=(n,x=n)=>n>1?--x<2||n%x&&f(n,x):0

This can easily be modified to calculate other things. For example, this 42-byte function counts the number of primes less than or equal to a number:

f=(n,x=n)=>n>1?n%--x?f(n,x):(x<2)+f(n-1):0

How it works

f = (         // Define a function f with these arguments:
  n,          //   n, the number to test;
  x = n       //   x, with a default value of n, the number to check for divisibility by.
) =>
  n > 1 ?     //   If n is greater than 1,
  --x < 2 ||  //     if x - 1 is less than 2, return true; otherwise,
  n % x &&    //     if n is divisible by x - 1, 0; otherwise,
  f(n, x)     //     the result of f(n, x - 1).
              //   This loops down through all numbers between n and 1,
              //     returning 0 if n is divisible by any of them.
  : 0         //   Otherwise (for all inputs less than 2), return 0.

You can golf this by 2 bytes if the input is guaranteed to be non-negative, and NaN can be returned for 1:

f=(n,x=n)=>n>1&--x<2||n%x&&f(n,x)

You can golf off 4 more bytes if the input is guaranteed to be greater than 1:

f=(n,x=n)=>--x<2||n%x&&f(n,x)

And one final byte if the output for primes can be any positive number:

f=(n,x=n)=>--x<2||n%x*f(n,x)

A grand total of 28 bytes!

Note: All of these fail when presented with a sufficiently large input, such as 12345.

Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240
Loading