Qbasicnews.com
Prime factors in 25 lines or less - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html)
+--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html)
+--- Thread: Prime factors in 25 lines or less (/thread-8202.html)

Pages: 1 2 3


Prime factors in 25 lines or less - whodat - 10-07-2005

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


Prime factors in 25 lines or less - Rattrapmax6 - 10-08-2005

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:


Prime factors in 25 lines or less - Agamemnus - 10-08-2005

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

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


from an ageing newbie - whodat - 10-09-2005

Everyone has a first post ...and I wish I were young enough to have homework! :o


Prime factors in 25 lines or less - Agamemnus - 10-09-2005

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

Tell us a little about your line of work..


Prime factors in 25 lines or less - whodat - 10-09-2005

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:


Prime factors in 25 lines or less - neuro - 10-09-2005

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


Prime factors in 25 lines or less - speedlemon - 10-09-2005

Quote: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.


Prime factors in 25 lines or less - Agamemnus - 10-09-2005

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



Prime factors in 25 lines or less - whodat - 10-09-2005

Quote:
whodat Wrote: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.