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
truthy
orfalsy
" or may we return inconsistent, butif-testable
, values? \$\endgroup\$