x86-64 Machine Code, 24 bytes
01 FF FF C7 6A 01 59 89 F8 FF C1 99 F7 F9 85 D2 75 F5 39 F9 0F 94 C0 C3
The above code defines a function that takes a single parameter (the input value, known to be a prime) in EDI (following the System V AMD64 calling convention used on Gnu/Unix), and returns a Boolean result in AL (1 if the input is a Sophie Germain prime, 0 otherwise).
The implementation is very similar to the solution to this challenge, since all we really have to do is determine whether a number is prime using as little code as possible, which means an inefficient iterative loop.
Basically, we take the input and immediately transform it into 2 × input + 1. Then, starting with a counter set to 2, we loop through and check to see if the counter is a factor. The counter is incremented each time through the loop. As soon as a factor is found, the loop ends and that factor is compared against 2 × input + 1. If the factor is equal to the test value, then that means we didn't find any smaller factors, and therefore the number must be prime. And since we have thus confirmed that 2 × input + 1 is prime, this means that input must be a Sophie Germain prime.
Ungolfed assembly language mnemonics:
IsSophieGermainPrime:
add edi, edi ; input *= 2
inc edi ; input += 1
push 1
pop rcx ; counter = 1
.CheckDivisibility:
inc ecx ; increment counter
mov eax, edi ; EAX = input (needs to be in EAX for IDIV; will be clobbered)
cdq ; sign-extend EAX into EDX:EAX
idiv ecx ; EDX:EAX / counter
test edx, edx ; check the remainder to see if divided evenly
jnz .CheckDivisibility ; keep looping if not divisible by this one
cmp ecx, edi ; compare counter to input
sete al ; set true if first found factor is itself;
ret ; otherwise, set false
Can I flip the True/False (i.e. output "False" if the prime is a SG prime, "True" otherwise)? – clismique – 2017-07-09T12:18:14.710
@Qwerp-Derp Of course, any value can be used as long as you specify it. – Mr. Xcoder – 2017-07-09T12:21:12.493
7Personally, I think ir would have been best to not gusrantee primality of the input. – Okx – 2017-07-09T13:47:56.823
1Does "returning two distinct values for each case" imply the values must be consistent rather than the tag text "either
truthyorfalsy" or may we return inconsistent, butif-testable, values? – Jonathan Allan – 2017-07-09T14:15:45.8876You should have a set of values for truthy and their complement for falsy, if you choose to have them inconsistent. Can I define SG primes as truthy values? :P – Dennis – 2017-07-09T14:51:08.903
@Dennis Of course... not – Mr. Xcoder – 2017-07-09T14:52:02.830
EDIT: The values must be consistent – Mr. Xcoder – 2017-07-09T14:52:55.583
That is not a problem if we use the tag text of "either
truthyorfalsey" though (this means the results must beif-testable, which, unless there is some eso-lang out there that has that bizarre feature, S.G. primes wont be). – Jonathan Allan – 2017-07-09T14:57:37.867.. See this meta-post by Peter Taylor regarding that definition.
– Jonathan Allan – 2017-07-09T15:03:36.767@JonathanAllan I will keep them consistent though – Mr. Xcoder – 2017-07-09T15:04:43.640
9
I'm not going to vote to close because I have a dupe hammer, but this seems like it's a dupe of Is it a prime?, since the only difference is that the input isn't the number you're testing. Adding the offset (2n + 1) is a pretty trivial modification.
– James – 2017-07-09T16:33:25.557@DJMcMayhem I think it is different though... That one also asks for a full program. – Mr. Xcoder – 2017-07-09T16:35:03.960
@DJMcMayhem this one checks for two numbers? – Nick T – 2017-07-09T16:45:03.823
@NickT No, the input is guaranteed to be prime – Mr. Xcoder – 2017-07-09T16:46:13.720