Recursive prime number f(x)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Recursive prime number f(x)

 
0
  #11
Jun 26th, 2004
The other problem with my code is that it is slow and uses a ton-o-stack. I don't see how a recursive function really buys enough in simplicity to warrent the stack usage and overhead of function calls.

Also, it would be better to start testing at 2 and going up, because half of all integers are divisible by 2, and a third are divisible by 3, and so on.

So, given that, I think your original code is pretty good. Run the loop up to sqrt(n) rather than n, and maybe (A) test for 2 as a special case, and (B) run the loop from 3 up and increment by 2 since no even numbers would be prime and this would save 50% of the tests worst-case.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 192
Reputation: Toba is an unknown quantity at this point 
Solved Threads: 5
Toba's Avatar
Toba Toba is offline Offline
Junior Poster

Re: Recursive prime number f(x)

 
0
  #12
Jun 26th, 2004
You know, I just remembered that I did this two years ago in QBASIC. Here's my code:

  1. 100 Cls
  2. 200 INPUT "WHAT NUMBER DO YOU WANT TO CHECK FOR PRIMENESS? ", NUMCHECK#
  3. 300 GoTo 500
  4. 400 Print "WHAT NUMBER DO YOU WHAT TO CHECK FOR PRIMENESS? ", NUMCHECK#
  5. 500 Print "PROCESSING..."
  6. 600 NUMCHECK# = Fix(NUMCHECK#)
  7. 550 MAXCHECK# = Sqr(NUMCHECK#)
  8. 700 NUMDIVD# = 2
  9. 800 QUOTIENT# = NUMCHECK# / NUMDIVD#
  10. 900 RNDDQTNT# = Fix(QUOTIENT#)
  11. 1000 If RNDDQTNT# = QUOTIENT# Then GoTo 1700
  12. 1100 NUMDIVD# = NUMDIVD# + 1
  13. 1200 GoTo 1400
  14. 1300 NUMDIVD# = NUMDIVD# + 2
  15. 1400 If NUMDIVD# < MAXCHECK# Then QUOTIENT# = NUMCHECK# / NUMDIVD# Else GoTo 2100
  16. 1500 RNDDQTNT# = Fix(QUOTIENT#)
  17. 1600 If RNDDQTNT# = QUOTIENT# Then GoTo 1700 Else GoTo 1300
  18. 1700 Print NUMCHECK#; "IS NOT PRIME"
  19. 1800 Print "DIVISIBLE BY"; QUOTIENT#
  20. 1900 Print "DIVISIBLE BY"; NUMDIVD#
  21. 2000 GoTo 2200
  22. 2100 Print NUMCHECK#; "IS PRIME"
  23. 2200 Beep
  24. 2250 Print ""
  25. 2300 Print " OPTIONS"
  26. 2400 Print "DO ANOTHER Y/N/1/2 "
  27. 2500 Print "PRESS y TO GO THROUGH THE ENTIRE PROGRAM AGAIN"
  28. 2600 Print "PRESS n TO QUIT"
  29. 2700 Print "PRESS 1 TO CHECK PRIMENESS OF FIRST FACTOR"
  30. 2800 Print "PRESS 2 TO CHECK PRIMENESS OF SECOND FACTOR"
  31. 2900 A$ = INKEY$
  32. 3000 If A$ = "" Then GoTo 2900
  33. 3100 If A$ = "n" Then GoTo 3800
  34. 3200 If A$ = "y" Then GoTo 100
  35. 3300 If A$ = "1" Then NUMCHECK# = QUOTIENT#: Cls: GoTo 400
  36. 3400 If A$ = "2" Then NUMCHECK# = NUMDIVD#: Cls: GoTo 400
  37. 3700 GoTo 2900
  38. 3800 End

I didn't seem to know how to do a normal loop back then, but it works (I think).
what? WHAT?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 4
Reputation: wolfstrike is an unknown quantity at this point 
Solved Threads: 0
wolfstrike wolfstrike is offline Offline
Newbie Poster

Re: Recursive prime number f(x)

 
0
  #13
Jun 27th, 2004
ahhh BASIC programming , when programming was easy and FUN.
<sniff> <sniff>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 15763 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC