Skip to content

Q70 #64

@Anding

Description

@Anding

I would like to respectfully suggest an answer for question 70. But there are always better ways, even if this may work, and I'd be grateful for such thoughts.

%assign limit 15			; limit can be no higher than 63 with this code
%assign primestring 0		; bit-string holding our primes 1=prime; 0=composite
%assign n 3
%rep limit
	%assign primeflag 1		; 1 = n is prime; 0 = n is composite		
	%assign i 1
	%rep n/2
		%assign i i+1
		%if n % i = 0
			%assign primeflag 0
			%exitrep
		%endif
	%endrep
	%assign primestring primestring | (primeflag << n)
	%assign n n+1
%endrep
	dq primestring
		
%define isprime(n) (primestring >> n) & 1
; test the function with some outputs
	db 15, isprime(15) ; expect 0
	db 17, isprime(17) ; expect 1

produces

$ nasm -E primes.asm
%line 17+1 primes.asm
 dq 141480

 db 15, (141480 >> 15) & 1 ; expect 0
 db 17, (141480 >> 17) & 1 ; expect 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions