Hi all I’m looking for SPSS code to generate ‘Prime Numbers’ between a given range (a lower bound and an upper bound to be input by the user). The output is to be saved in an SPSS file as it is generated (to be saved in the C: drive). I’d really appreciate this as it is beyond my programming skills. Thanks a lot. Jaspal Cheema
|
Administrator
|
SPSS syntax is really not designed for such an exercise.
If one were a unrelenting masochist it could likely be done but I'm not going to bother. There is code in other languages scattered all over the net in different languages. Do a little googling and you can probably find a python implementation which can be readily called within SPSS.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
Ah, to hell with it ;-)
Here is a MATRIX program implementing http://umumble.com/blogs/algorithm/334/ If you are working with *large* numbers then this is probably not the way to go. --- SET MXLOOPS 250000. MATRIX. COMPUTE P=MAKE(250000,1,1). COMPUTE TOP=NROW(P). COMPUTE PRIMES={1}. LOOP #=2 TO TOP. + DO IF P(#) NE 0. + COMPUTE PRIMES={PRIMES;#}. + LOOP ##=2*# TO TOP BY #. + COMPUTE P(##,1)=0. + END LOOP. + END IF. END LOOP. **PRINT PRIMES. SAVE PRIMES/OUTFILE * / VARIABLES=PRIME. END MATRIX.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
LOL...I had a feeling you'd not be able to resist taking a crack at it! Nicely done. ;-)
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Hi, Thank you for contacting Reputation Institute’s MSG. The office is closed for Memorial Day Monday May 28. I will respond to your email when we're back on Tuesday May 29. Best, Jenny |
In reply to this post by Cheema, Jaspal (MCYS)
With SPSS 20 and Essentials for R
The prime function is from: http://www.r-bloggers.com/prime-number-in-r-language-cloudstat/ BEGIN PROGRAM R. prime = function(n) { n = as.integer(n) if(n > 1e8) stop("n too large") primes = rep(TRUE, n) primes[1] = FALSE last.prime = 2L fsqr = floor(sqrt(n)) while (last.prime <= fsqr) { primes[seq.int(2L*last.prime, n, last.prime)] = FALSE sel = which(primes[(last.prime+1):(fsqr+1)]) if(any(sel)){ last.prime = last.prime + min(sel) }else last.prime = fsqr+1 } which(primes) } # To get all primes less than 100 prime(100) # To get primes from the fifth prime to the tenth prime primes2 <- prime(100) primes2[5:10] # To get the fifth, the seventh and the tenth primes primes2[c(5,7,10)] # To get all primes less than 100 but the fifth and the tenth primes primes2[-c(5,10)] end program. Len On Fri, May 25, 2012 at 2:13 PM, Cheema, Jaspal (MCYS) <[hidden email]> wrote: > Hi all > > I’m looking for SPSS code to generate ‘Prime Numbers’ between a given range > (a lower bound and an upper bound to be input by the user). > > The output is to be saved in an SPSS file as it is generated (to be saved in > the C: drive). > > I’d really appreciate this as it is beyond my programming skills. > > Thanks a lot. > > Jaspal Cheema ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
In reply to this post by Cheema, Jaspal (MCYS)
The saving part was missing in my previous message.
BEGIN PROGRAM R. prime = function(n) { n = as.integer(n) if(n > 1e8) stop("n too large") primes = rep(TRUE, n) primes[1] = FALSE last.prime = 2L fsqr = floor(sqrt(n)) while (last.prime <= fsqr) { primes[seq.int(2L*last.prime, n, last.prime)] = FALSE sel = which(primes[(last.prime+1):(fsqr+1)]) if(any(sel)){ last.prime = last.prime + min(sel) }else last.prime = fsqr+1 } which(primes) } # To get all primes less than 100 prime(100) # To get primes from the fifth prime to the tenth prime primes2 <- prime(100) primes2[5:10] # To get the fifth, the seventh and the tenth primes primes2[c(5,7,10)] # To get all primes less than 100 but the fifth and the tenth primes primes2[-c(5,10)] # The following code will save the primes to the C: drive in a text file, # together with a code file to read the text file from SPSS p2 <- primes2[5:10] options(stringsAsFactors = FALSE) mydata <- data.frame(p2) library(foreign) write.foreign(mydata, "c:/mydata.txt", "c:/mydata.sps", package="SPSS") end program. Len On Fri, May 25, 2012 at 2:13 PM, Cheema, Jaspal (MCYS) <[hidden email]> wrote: > Hi all > > I’m looking for SPSS code to generate ‘Prime Numbers’ between a given range > (a lower bound and an upper bound to be input by the user). > > The output is to be saved in an SPSS file as it is generated (to be saved in > the C: drive). > > I’d really appreciate this as it is beyond my programming skills. > > Thanks a lot. > > Jaspal Cheema ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Administrator
|
In reply to this post by David Marso
Some hopefully obvious optimizations ;-))
Trick question: What are they? ----------------- DEFINE PRIMES (N !TOKENS(1)). PRESERVE. SET MXLOOPS !N. MATRIX. + COMPUTE X=MAKE(!N,1,1). + COMPUTE TOP=NROW(X). + COMPUTE TOP2=TRUNC(SQRT(TOP)). + COMPUTE #PRIMES=MAKE(TRUNC(!N/10),1,0). + COMPUTE #PRIMES(1)=2. + COMPUTE #NPrime=2. + LOOP #=3 TO TOP BY 2. + DO IF (X(#) EQ 1). + DO IF (# LT TOP2). + LOOP ##=2*# TO TOP BY #. + COMPUTE X(##)=0. + END LOOP. + END IF. + COMPUTE #PRIMES(#NPrime)=#. + COMPUTE #NPrime=#NPrime+1. + DO IF #NPrime GT NROW(#PRIMES). + COMPUTE #PRIMES={#PRIMES;MAKE(TRUNC(NROW(#PRIMES)/4),1,0)}. + PRINT #Nprime. + END IF. + END IF. + END LOOP. SAVE #PRIMES/OUTFILE * / VARIABLES=PRIMEOLD. END MATRIX. RESTORE. SELECT IF PRIMEOLD > 0. EXE. !ENDDEFINE. PRIMES N=500000.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
You appear to be well primed for Memorial Day, David. :-|
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
Also introduced a teensy bug. Can you find it?
--
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |