Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Coding puzzle 3
Message
De
06/06/2002 18:55:02
 
 
À
06/06/2002 14:56:09
Walter Meester
HoogkarspelPays-Bas
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Titre:
Divers
Thread ID:
00665235
Message ID:
00665734
Vues:
24
WOW Walter, it's 4 times faster than the routine I sent! I wonder whether we can use this routine to find the next unknown prime number!

>Hoi peter, Nadya
>
>>
>>create cursor prime ( primenumber i )
>>insert into prime value ( 3 )
>>
>>MaxRange = 100000
>>
>>t = seconds()
>>for i = 5 to MaxRange step 2
>>  if IsPrime(i)
>>	insert into prime value ( i )
>>  endif
>>next
>>? seconds() - t
>>
>>func isprime
>>	lpar i
>>	local n
>>	scan
>>		n = prime.primenumber
>>		if int( i / n ) = i / n
>>			return .f.
>>		endif
>>		if n^2 > i
>>			exit
>>		endif
>>	endscan
>>	return .t.
>>
>
>What about
>
>
>CREATE CURSOR Prime (primenumber i)
>INSERT INTO Prime VALUES (3)
>
>FOR I = 5 TO 100000 STEP 2
>	nSQRT = SQRT(i)
>	GO TOP
>	LOCATE WHILE prime.primenumber =< nSQRT FOR I % prime.primenumber = 0
>	IF !FOUND()
>		INSERT INTO Prime VALUES ( i )
>	ENDIF
>ENDFOR
>
>
>Long live the fast local data engine !! I wonder if VB can do this as fast as VFP.
>So the whole routine Nadya requested can look like the following:
>
>
>SET TALK OFF
>SELECT Numbers
>GO BOTTOM
>MaxRange = Numbers.Num
>
>** Generate Prime numbers
>
>CREATE CURSOR Prime (primenumber i)
>INSERT INTO Prime VALUES (3)
>
>FOR I = 5 TO MaxRange STEP 2
>	nSQRT = SQRT(i)
>	GO TOP
>	LOCATE WHILE prime.primenumber =< nSQRT FOR I % prime.primenumber = 0
>	IF !FOUND()
>		INSERT INTO Prime VALUES ( i )
>	ENDIF
>ENDFOR
>INDEX ON PrimeNumber TAG Prime
>
>** Determine the missing primenumbers in the gaps in the Numtable
>
>CREATE CURSOR Invert (un I)
>SELECT Numbers
>GO TOP
>
>nLow = Num
>SCAN
>	FOR nT = nLow+1 TO Num-1
>		IF KEYMATCH(nT,1,"Prime")
>			INSERT INTO Invert VALUES (nT)
>		ENDIF
>	ENDFOR
>	nLow=Num
>ENDSCAN
>
>
>Walter,
Groet,
Peter de Valença

Constructive frustration is the breeding ground of genius.
If there’s no willingness to moderate for the sake of good debate, then I have no willingness to debate at all.
Let's develop superb standards that will end the holy wars.
"There are three types of people: Alphas and Betas", said the beta decisively.
If you find this message rude or offensive or stupid, please take a step away from the keyboard and try to think calmly about an eventual a possible alternative explanation of my message.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform