| | |
Recursive prime number f(x)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
You know, I just remembered that I did this two years ago in QBASIC. Here's my code:
I didn't seem to know how to do a normal loop back then, but it works (I think).
C Syntax (Toggle Plain Text)
100 Cls 200 INPUT "WHAT NUMBER DO YOU WANT TO CHECK FOR PRIMENESS? ", NUMCHECK# 300 GoTo 500 400 Print "WHAT NUMBER DO YOU WHAT TO CHECK FOR PRIMENESS? ", NUMCHECK# 500 Print "PROCESSING..." 600 NUMCHECK# = Fix(NUMCHECK#) 550 MAXCHECK# = Sqr(NUMCHECK#) 700 NUMDIVD# = 2 800 QUOTIENT# = NUMCHECK# / NUMDIVD# 900 RNDDQTNT# = Fix(QUOTIENT#) 1000 If RNDDQTNT# = QUOTIENT# Then GoTo 1700 1100 NUMDIVD# = NUMDIVD# + 1 1200 GoTo 1400 1300 NUMDIVD# = NUMDIVD# + 2 1400 If NUMDIVD# < MAXCHECK# Then QUOTIENT# = NUMCHECK# / NUMDIVD# Else GoTo 2100 1500 RNDDQTNT# = Fix(QUOTIENT#) 1600 If RNDDQTNT# = QUOTIENT# Then GoTo 1700 Else GoTo 1300 1700 Print NUMCHECK#; "IS NOT PRIME" 1800 Print "DIVISIBLE BY"; QUOTIENT# 1900 Print "DIVISIBLE BY"; NUMDIVD# 2000 GoTo 2200 2100 Print NUMCHECK#; "IS PRIME" 2200 Beep 2250 Print "" 2300 Print " OPTIONS" 2400 Print "DO ANOTHER Y/N/1/2 " 2500 Print "PRESS y TO GO THROUGH THE ENTIRE PROGRAM AGAIN" 2600 Print "PRESS n TO QUIT" 2700 Print "PRESS 1 TO CHECK PRIMENESS OF FIRST FACTOR" 2800 Print "PRESS 2 TO CHECK PRIMENESS OF SECOND FACTOR" 2900 A$ = INKEY$ 3000 If A$ = "" Then GoTo 2900 3100 If A$ = "n" Then GoTo 3800 3200 If A$ = "y" Then GoTo 100 3300 If A$ = "1" Then NUMCHECK# = QUOTIENT#: Cls: GoTo 400 3400 If A$ = "2" Then NUMCHECK# = NUMDIVD#: Cls: GoTo 400 3700 GoTo 2900 3800 End
I didn't seem to know how to do a normal loop back then, but it works (I think).
what? WHAT?
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: program help
- Next Thread: Assign content to a function pointer
Views: 15763 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi





