BookmarkSubscribeRSS Feed
PurushReddy
Fluorite | Level 6
I want prime numbers from 1 to 1000
Please explain the logic with using mod function
8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

What have you tried so far? The community has many many examples of code that does this already 🙂

Kurt_Bremser
Super User

You don't need the mod() function, you use the simple elimination method:

data primes;
array primes {1000} _temporary_ (1000*1);
do i = 2 to 1000;
  if primes{i} then do j = 2 * i to 1000 by i;
    primes{j} = 0;
  end;
end;
do number = 2 to 1000;
  if primes{number} then output;
end;
keep number;
run;
yabwon
Onyx | Level 15
With 1 small modification:

do number = 2 to 1000;
if primes{number} then output;
end;

All the best
Bart
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User
Kurt,
To make it faster, code could change it into

do i = 2 to int(sqrt(1000));
Kurt_Bremser
Super User

Revised code, made dynamic and optimized:

%let max=1000000;

data primes;
array primes {&max.} _temporary_ (&max.*1);
do i = 2 to int(sqrt(&max.));
  if primes{i} then do j = 2 * i to &max. by i;
    primes{j} = 0;
  end;
end;
do number = 2 to &max.;
  if primes{number} then output;
end;
keep number;
run;

I used a million because before that, there was no measurable difference between with and without the sqrt function. With a million, the difference was .06 seconds to .10 seconds in favor of using the function, using UE on a MacBook Pro.

yabwon
Onyx | Level 15

@Kurt_Bremser 

 

Kurt,

 

If you make it:

  array primes {&max.} _temporary_;
  do number = 1 to &max.;
    primes{number} = 1;
  end;

you will reduce memory by 50%.

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

There seems to be slight tradeoff (the additional do loop added another .01 seconds), but this is a valid point when &max is increased by several orders of magnitude.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 974 views
  • 6 likes
  • 5 in conversation