Qbasicnews.com
July 02, 2020, 06:49:26 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Prime factors in 25 lines or less  (Read 12091 times)
whodat
Member
*
Posts: 31


« on: October 07, 2005, 02:21:34 PM »

In 25 lines or less, write a program in QBasic that lists the prime factors of any positive integer.  The program prompts the user for a number, then prints to the screen: "The prime factors are:", and then lists them.  If the entered number is already prime, it will be the only number listed. The number 1 is not considered prime.
Statements separated by colons are considered separate lines.

For checking purposes, if you enter 987654321, the list should read: 3,3,17,17,379721
Logged
Rattrapmax6
__/--\__
*****
Posts: 2577



WWW
« Reply #1 on: October 07, 2005, 04:00:41 PM »

You might want to have a no : rule,. other wise someone could do it in one line.... At least with FBIde or NotePad,. I think QB's IDE had a 260 cross limit or something...  :wink:

Edit: Oops, sorry, you did, I missed that line...  :oops:
Logged

Kevin (x.t.r.GRAPHICS)

Agamemnus
x/ \z
*****
Posts: 3491



« Reply #2 on: October 07, 2005, 11:05:56 PM »

Someone with one post? Smells like homework... NM my solution..edited.

I think I'll wait a day or so..
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
whodat
Member
*
Posts: 31


« Reply #3 on: October 08, 2005, 06:41:45 PM »

Everyone has a first post ...and I wish I were young enough to have homework! Shocked
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #4 on: October 08, 2005, 06:49:48 PM »

Well, your words, your stringent output conditions, and your spelling suggests otherwise. . . . . .

Tell us a little about your line of work..
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
whodat
Member
*
Posts: 31


« Reply #5 on: October 08, 2005, 07:08:46 PM »

My words, my spelling...Ah!, I see.  You're one of those arrogant nerds who hides behind a computer monitor and snipes at others.  I think that my best course is to ignore your words.     :barf:
Logged
neuro
Forum Regular
**
Posts: 114



WWW
« Reply #6 on: October 08, 2005, 07:35:03 PM »

Code:

INPUT "enter a number: ", x&
PRINT "the prime factors are:";
k& = 2
WHILE k& <= x&
  WHILE x& MOD k& = 0
    x& = x& / k&
    PRINT k&;
  WEND
  k& = k& + 1
WEND
PRINT


 - neuro
Logged

ignatures suck
speedlemon
I hold this place together
*****
Posts: 874



« Reply #7 on: October 08, 2005, 07:51:48 PM »

Quote from: "whodat"
My words, my spelling...Ah!, I see.  You're one of those arrogant nerds who hides behind a computer monitor and snipes at others.  I think that my best course is to ignore your words.     :barf:

...
Agamemnus is just making an observation about your language and you should give a little more respect to him.  I had a chance to get to know him a few years ago and he turned out to be a pretty nice (and helpful) guy.
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #8 on: October 08, 2005, 08:11:54 PM »

That wasn't very nice.

Did I call you an arrogant noob?

No...

Anyway, I posted mine in the FAQ, so I guess I'll post it again since neuro volunteered his code..

Code:

CLS
DIM testfactor&(1 TO 100)
DIM isfactored%(1 TO 100)
n% = 1
newfree% = 1
INPUT "What is the number you want to factor"; testfactor&(n%)
DO
if testfactor%(n%) = 0 THEN EXIT DO

FOR testdiv& = 2 TO testfactor%(n%) ^ .5
'There is a more efficient way to do \ and /, but I don't remember it.
result1& = testfactor&(n%) \ testdiv&
IF result1& = testfactor&(n%) / testdiv& THEN
isfactored%(n%) = 1
testfactor&(newfree% + 1) = result1&
testfactor&(newfree% + 2) = testdiv&
newfree% = newfree% + 2
EXIT FOR
END IF
NEXT testdiv&
n% = n% + 1
LOOP

FOR i% = 1 TO newfree%
IF isfactored%(i%) = 0 THEN PRINT testfactor&(i%)
next i%
SLEEP
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
whodat
Member
*
Posts: 31


« Reply #9 on: October 08, 2005, 09:41:37 PM »

Quote from: "speedlemon"
Quote from: "whodat"
My words, my spelling...Ah!, I see.  You're one of those arrogant nerds who hides behind a computer monitor and snipes at others.  I think that my best course is to ignore your words.     :barf:

...
Agamemnus is just making an observation about your language and you should give a little more respect to him.  I had a chance to get to know him a few years ago and he turned out to be a pretty nice (and helpful) guy.


Ok, he's a nice guy.  He just had a rather peculiar way of introducing himself.
Logged
whodat
Member
*
Posts: 31


« Reply #10 on: October 08, 2005, 09:57:33 PM »

Quote from: "neuro"
Code:

INPUT "enter a number: ", x&
PRINT "the prime factors are:";
k& = 2
WHILE k& <= x&
  WHILE x& MOD k& = 0
    x& = x& / k&
    PRINT k&;
  WEND
  k& = k& + 1
WEND
PRINT


 - neuro



Only 11 lines.  My code has 24!   :oops:


DEFLNG A-Z
CLS
INPUT "Enter positive integer"; fact
PRINT "Prime Factors are: ";
srfact = INT(SQR(fact))
DO
  prime = prime + 2
  srprime = INT(SQR(prime))
  FOR a = 2 TO srprime
    IF prime MOD a = 0 THEN EXIT FOR
  NEXT a
  IF a = srprime + 1 THEN
    DO
      k = fact MOD prime
      IF k = 0 THEN
        PRINT prime;
        fact = fact / prime
      END IF
    LOOP UNTIL k > 0
  END IF
  IF prime = 2 THEN prime = 1
  IF fact = 1 THEN EXIT DO
LOOP UNTIL prime >= srfact
IF fact <> 1 THEN PRINT fact
Logged
whodat
Member
*
Posts: 31


« Reply #11 on: October 08, 2005, 10:06:01 PM »

I can't seem to get Agamemnus's code to work.  It just parrots back the number that I enter.[/b]
Logged
rpgfan3233
Ancient Guru
****
Posts: 617



« Reply #12 on: October 09, 2005, 02:27:44 AM »

I would post mine, but it's too similar to neuro's (I think it's the fastest way too). Basically, the only differences are that I used different variable names and DO...WHILE loops rather than WHILE...WEND.

Edit:
Quote from: "whodat"
I can't seem to get Agamemnus's code to work.  It just parrots back the number that I enter.[/b]

Are you entering a composite number or a prime number when you get "The Parrot Effect"? :lol:
Logged

974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #13 on: October 09, 2005, 02:24:58 PM »

Neuro's entry takes ages to factorize 2123456783, it issues a 7 at the start then it stands for a while.
I tried to modify it to make it faster, by not checking 2 and 3 multiples and stopping when the factor is equal to the square root of the remainder. Here is the result, 25 lines at all.

Code:

declare sub checkf(k&)
dim shared x&
do
  print
INPUT "enter a number to factorize[0 to end] : ", x&
if x&=0 then exit do
PRINT "the prime factors are:";
checkf(2)
checkf(3)
a&=2
k& = 3 + a&
WHILE k& <=int(sqr(x&))
 checkf(k&)
 k& = k& + a&
 a&=6&-a&
WEND
if x&>1 then print x&;
loop
PRINT "Ended"
sub checkf(k&)
WHILE x& MOD k& = 0
  x& = x& \ k&
  PRINT k&;
WEND
end sub
Logged

Antoni
d.j.peters
Forum Regular
**
Posts: 147



WWW
« Reply #14 on: October 09, 2005, 03:17:43 PM »

Hello Antoni,
realy nice work.

Joshy
Logged

[size=0]sorry about my english[/size]
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!