Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Coding puzzle 3
Message
From
06/06/2002 07:57:20
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
00665235
Message ID:
00665349
Views:
29
>I meant prime numbers, just didn't know the correct term. E.g. each number has only two dividors (1 and itself).
>
>1
>2
>3
>5
>7
>11
>13
>etc.

It is convenient to have a UDF that returns .T. if the input is a prime number.

An algorithm that is reasonable in terms of speed and simplicity, for numbers up to several millions, is:

Divide input by 2

Divide input by all odd numbers, from 3 to sqrt(input)

Return .F. immediately if any of the divisions gives no remainder.

You can then search for each prime number with a loop, also starting at 2, and then:
for i = 3 to MaxRange step 3
  if not seek(i) and IsPrime(i)
    insert into...
  endif
next
Hilmar.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform