All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Numbers
DIY prime magic (Posted on 2012-01-13) Difficulty: 4 of 5
In a 4x4 magic square all rows, columns and the two main diagonals are distinct 4-digit prime numbers when read in either direction.

Create such a square.

Bonus (d4): same for order-5 magic square and 5-digit primes.

Based on L. E. Card,Patterns in Primes, JRM 1:2, 1968, pp .93-99,

See The Solution Submitted by Ady TZIDON    
No Rating

Comments: ( Back to comment list | You must be logged in to post comments.)
re(3): From the computer -semi-spoiler | Comment 9 of 10 |
(In reply to re(2): From the computer -semi-spoiler by brianjn)

Brian, you could speed up the prime test to make the program run faster.

The current prime test checks every number below the number being tested:

SUB primetest
prm = 0
s = 2
DO
 IF (chk MOD s = 0) THEN prm = 1
 s = s + 1
LOOP WHILE (s < chk AND prm = 0)

END SUB

First of all you need go only to (and including) the square root of the number under test; any factor above that would have a corresponding factor below it, or in the case of the square root itself just the square root of any perfect square.

Second, if you tested division by 2, and then went into a DO loop, you'd be able to increment by 2 rather than 1 and halve the number of tests. In fact, the below factoring program tests division by 2, 3 and 5 before starting off the loop with 7; from then on it only tests dividing by 8 out of every 30 numbers. Of course when seeking only primality, as soon as you obtained a factor lower than the number itself, you'd get out of the loop and declare the argument non-prime, rather than go on to find other factors.

 

SUB factor (num, s$)
 s$ = "": n = ABS(num): IF n > 0 THEN limit = sqroot(n):  ELSE limit = 0
 IF limit <> INT(limit) THEN limit = INT(limit + 1)
 dv = 2: GOSUB DivideIt
 dv = 3: GOSUB DivideIt
 dv = 5: GOSUB DivideIt
 dv = 7
 DO UNTIL dv > limit
   GOSUB DivideIt: dv = dv + 4 '11
   GOSUB DivideIt: dv = dv + 2 '13
   GOSUB DivideIt: dv = dv + 4 '17
   GOSUB DivideIt: dv = dv + 2 '19
   GOSUB DivideIt: dv = dv + 4 '23
   GOSUB DivideIt: dv = dv + 6 '29
   GOSUB DivideIt: dv = dv + 2 '31
   GOSUB DivideIt: dv = dv + 6 '37
   IF INKEY$ = CHR$(27) THEN s$ = CHR$(27): EXIT SUB
 LOOP
 IF n > 1 THEN s$ = s$ + STR$(n)
 EXIT SUB

DivideIt:
 DO
  q = INT(n / dv)
  IF q * dv = n AND n > 0 THEN
    n = q: s$ = s$ + STR$(dv): IF n > 0 THEN limit = sqroot(n):  ELSE limit = 0
    IF limit <> INT(limit) THEN limit = INT(limit + 1)
   ELSE
    EXIT DO
  END IF
 LOOP
 RETURN
END SUB


  Posted by Charlie on 2012-01-21 11:02:43
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (1)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (6)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information