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

Home > Numbers
Is it, or is it not? (Posted on 2007-04-10) Difficulty: 4 of 5
Is (258+1)/5 an integer? A prime?

See The Solution Submitted by Federico Kereki    
Rating: 3.6667 (3 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
re: Just a simple pattern | Comment 10 of 12 |
(In reply to Just a simple pattern by Nishant)

This program factors a number into its constituent primes, and if the number itself is prime, just returns the same number:

   4   loop
   5   input Num
  10     S$="":N=abs(Num)
  20     if N>0 then Limit=sqrt(N):else Limit=0
  30     if Limit<>int(Limit) then Limit=int(Limit+1)
  40     Dv=2:gosub *DivideIt
  50     Dv=3:gosub *DivideIt
  60     Dv=5:gosub *DivideIt
  70     Dv=7
  80     loop
  90      if Dv>Limit then goto *Afterloop
 100      gosub *DivideIt:Dv=Dv+4 '11
 110      gosub *DivideIt:Dv=Dv+2 '13
 120      gosub *DivideIt:Dv=Dv+4 '17
 130      gosub *DivideIt:Dv=Dv+2 '19
 140      gosub *DivideIt:Dv=Dv+4 '23
 150      gosub *DivideIt:Dv=Dv+6 '29
 160      gosub *DivideIt:Dv=Dv+2 '31
 170      gosub *DivideIt:Dv=Dv+6 '37
 180      if inkey=chr(27) then S$=chr(27):end
 190    endloop
 200    *Afterloop
 210    if N>1 then S$=S$+str(N)
 220    print S$
 230   endloop
 240
 250   *DivideIt
 260    loop
 270     Q=int(N/Dv)
 280     if Q*Dv=N and N>0 then
 290       :N=Q:S$=S$+str(Dv)
 300       :if N>0 then Limit=sqrt(N):else Limit=0:endif
 310       :if Limit<>int(Limit) then Limit=int(Limit+1):endif
 320      :else
 330      :goto *Afterdo
 340     :endif
 350    endloop
 360    *Afterdo
 370    return

That was in the UBASIC language.  In QuickBasic, the following subroutine performes the same function, placing a string into s$ that contains the prime factors of num:

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 2007-04-11 09:18:17
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


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

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