saved some chars
Source Link
algorithmshark
  • 8.5k
  • 2
  • 29
  • 53

J - 3535 32 char

"Prompt the user" is the bane of every J golfer. There go 8 ofall my hard-earned characters!

p:(n,n)#:i.&n,+/~(*1&p~p:)i.n=:".1!:1]1

Explained:

  • ".1!:1]1 - Read in a string (1!:1) from input (file handle 1) and convert it to a number (".).
  • p:i.n=: - Assign this number to the variable n, and then take the list of integers less thanfirst n.
  • (*1&p:) - Multiply each number by 1 if it is prime, and 0 if it isn't primes.
  • +/~ - Make an addition table, n wide and n high.
  • i.&n, - Turn the table into a single list, and then find the index of the first occurrence of n, which exists if Goldbach's conjecture is true.
  • p:(n,n)#: - Retrieve the row and column from the index, and take the corresponding primes.

Usage:

   p:(n,n)#:i.&n,+/~(*1&p~p:)i.n=:".1!:1]1
666
5 661
   p:(n,n)#:i.&n,+/~(*1&p~p:)i.n=:".1!:1]1
1024
3 1021

Had the prompt not been a requirement, here's the 27a 25 character verb:

(,~#~p:@#:]i.~[:~&,+/~@(*1&p:)p:@i.)

J - 35 char

"Prompt the user" is the bane of every J golfer. There go 8 of my characters!

(n,n)#:i.&n,+/~(*1&p:)i.n=:".1!:1]1

Explained:

  • ".1!:1]1 - Read in a string (1!:1) from input (file handle 1) and convert it to a number (".).
  • i.n=: - Assign this number to the variable n, and then take the list of integers less than n.
  • (*1&p:) - Multiply each number by 1 if it is prime, and 0 if it isn't.
  • +/~ - Make an addition table, n wide and n high.
  • i.&n, - Turn the table into a single list, and then find the index of the first occurrence of n, which exists if Goldbach's conjecture is true.
  • (n,n)#: - Retrieve the row and column from the index.

Usage:

   (n,n)#:i.&n,+/~(*1&p:)i.n=:".1!:1]1
666
5 661
   (n,n)#:i.&n,+/~(*1&p:)i.n=:".1!:1]1
1024
3 1021

Had the prompt not been a requirement, here's the 27 character verb:

(,~#:]i.~[:,+/~@(*1&p:)@i.)

J - 35 32 char

"Prompt the user" is the bane of every J golfer. There go all my hard-earned characters!

p:(n,n)#:i.&n,+/~p:i.n=:".1!:1]1

Explained:

  • ".1!:1]1 - Read in a string (1!:1) from input (file handle 1) and convert it to a number (".).
  • p:i.n=: - Assign this number to the variable n, and then take the first n primes.
  • +/~ - Make an addition table, n wide and n high.
  • i.&n, - Turn the table into a single list, and then find the index of the first occurrence of n, which exists if Goldbach's conjecture is true.
  • p:(n,n)#: - Retrieve the row and column from the index, and take the corresponding primes.

Usage:

   p:(n,n)#:i.&n,+/~p:i.n=:".1!:1]1
666
5 661
   p:(n,n)#:i.&n,+/~p:i.n=:".1!:1]1
1024
3 1021

Had the prompt not been a requirement, here's a 25 character verb:

(,~p:@#:]i.~&,+/~@:p:@i.)
Source Link
algorithmshark
  • 8.5k
  • 2
  • 29
  • 53

J - 35 char

"Prompt the user" is the bane of every J golfer. There go 8 of my characters!

(n,n)#:i.&n,+/~(*1&p:)i.n=:".1!:1]1

Explained:

  • ".1!:1]1 - Read in a string (1!:1) from input (file handle 1) and convert it to a number (".).
  • i.n=: - Assign this number to the variable n, and then take the list of integers less than n.
  • (*1&p:) - Multiply each number by 1 if it is prime, and 0 if it isn't.
  • +/~ - Make an addition table, n wide and n high.
  • i.&n, - Turn the table into a single list, and then find the index of the first occurrence of n, which exists if Goldbach's conjecture is true.
  • (n,n)#: - Retrieve the row and column from the index.

Usage:

   (n,n)#:i.&n,+/~(*1&p:)i.n=:".1!:1]1
666
5 661
   (n,n)#:i.&n,+/~(*1&p:)i.n=:".1!:1]1
1024
3 1021

Had the prompt not been a requirement, here's the 27 character verb:

(,~#:]i.~[:,+/~@(*1&p:)@i.)