Its not homework . Basically i want to get easy solution:
I am trying this for ist problem:
n= seq(1,100)
for (j in n:100) {
f = 1
i = 2
n = j
while (i <= n / 2) {
if (n %% i == 0) {
f = 0
break
}
i = i + 1
}
if (f == 1) {
print(paste("Number is prime :", n))
}
}
On Wed, Oct 27, 2021 at 1:35 AM Rolf Turner <r.turner at auckland.ac.nz>
wrote:
>
> On Wed, 27 Oct 2021 01:09:50 +0500
> Anas Jamshed <anasjamshed1994 at gmail.com> wrote:
>
> > I need help to these questions
> >
> > ### Question 1
> > Create a variable containing a sequence of numbers from 1 to 100:
> >
> > Iterate over the variables and print those numbers which are prime.
> >
> >
> > ### Question 2
> > Create a matrix of size 3x3 called mat_1:
> >
> > Iterate over all the values one by one and print the element as well
> > as the position in the matrix (row, col)
>
> You really should do your own homework.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
>
[[alternative HTML version deleted]]
Well, OK. I'll provide you an alternative and explanation.
The way you are going about it would work, but it is usually not the best
approach in R. Typically, the key in R is to whenever possible use whole
object manipulation rather than iteration. This hides the iteration details
and often pushes them down to the much more efficient underlying C code. So
here's one way to do it in R:
findprm <- function(n){
nn <- seq.int(2, n)
i <- 2
while(i <= floor(sqrt( n))){
nn <- nn[(nn <= i) | (nn %% i > 0)]
i <- nn[nn > i][1]
}
nn
}
Example:> findprm(66)
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
I'll leave it to you to work out the details for how it works. :-)
And, of course, there are almost certainly even better ways to do this, but
this should get you started.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Oct 26, 2021 at 1:59 PM Anas Jamshed <anasjamshed1994 at
gmail.com>
wrote:
> Its not homework . Basically i want to get easy solution:
> I am trying this for ist problem:
>
> n= seq(1,100)
>
> for (j in n:100) {
> f = 1
> i = 2
> n = j
> while (i <= n / 2) {
> if (n %% i == 0) {
> f = 0
> break
> }
> i = i + 1
> }
> if (f == 1) {
> print(paste("Number is prime :", n))
> }
> }
>
> On Wed, Oct 27, 2021 at 1:35 AM Rolf Turner <r.turner at
auckland.ac.nz>
> wrote:
>
> >
> > On Wed, 27 Oct 2021 01:09:50 +0500
> > Anas Jamshed <anasjamshed1994 at gmail.com> wrote:
> >
> > > I need help to these questions
> > >
> > > ### Question 1
> > > Create a variable containing a sequence of numbers from 1 to 100:
> > >
> > > Iterate over the variables and print those numbers which are
prime.
> > >
> > >
> > > ### Question 2
> > > Create a matrix of size 3x3 called mat_1:
> > >
> > > Iterate over all the values one by one and print the element as
well
> > > as the position in the matrix (row, col)
> >
> > You really should do your own homework.
> >
> > cheers,
> >
> > Rolf Turner
> >
> > --
> > Honorary Research Fellow
> > Department of Statistics
> > University of Auckland
> > Phone: +64-9-373-7599 ext. 88276
> >
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
There can be people doing homework for a course and as noted, the normal expectation is to use the resources provided including classroom instruction (or the often ZOOM or recordings) as well as textbooks. Forums like this are not a substitute and some nice people will sometimes volunteer not to do homework but help someone a bit such as asking them to think about the problem, what data structures or loops might be needed OR help them understand an error message or after seeing their attempts, point out a subtle flaw. There are some who are teaching themselves and are not being graded but are trying little project to learn. But again, it is best they learn for themselves and not be handed an answer. Some mailing lists have rules and perhaps might answer someone more if it is WORK question like how to fine tune a graph after they have most of it working. So, I am looking at the follow-up below with a jaundiced eye as I just saw something similar enough being asked on a Python board. The first question strikes me as odd because it contains a completely un-necessary looking part.> > ### Question 1 > > Create a variable containing a sequence of numbers from 1 to 100: > > > > Iterate over the variables and print those numbers which are prime.You do not really need to create a sequence and then again loop over the same sequence. The following code is shown: n= seq(1,100) for (j in n:100) { Well, the look could have been using n directly or using 1:100 but uses nonsense. To say n:100 requires n to be an integer, not some kind of sequence or vector. And why go to 200 in any case? The rest gets worse and worse with oddities like using just letters of the alphabet without meanings as variables, using integers like "f = 1" rather than Booleans for such flags, and using the "=" operator rather than the more accepted "<-" operator. The loop uses variable j then ignores it and keeps manipulating i. Someone wanting help might want to let people know what the algorithm is supposed to do. I won't try to guess and certainly won't supply a valid solution here! -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Anas Jamshed Sent: Tuesday, October 26, 2021 4:39 PM To: Rolf Turner <r.turner at auckland.ac.nz> Cc: R-help Mailing List <r-help at r-project.org> Subject: Re: [R] Need help in R Its not homework . Basically i want to get easy solution: I am trying this for ist problem: n= seq(1,100) for (j in n:100) { f = 1 i = 2 n = j while (i <= n / 2) { if (n %% i == 0) { f = 0 break } i = i + 1 } if (f == 1) { print(paste("Number is prime :", n)) } } On Wed, Oct 27, 2021 at 1:35 AM Rolf Turner <r.turner at auckland.ac.nz> wrote:> > On Wed, 27 Oct 2021 01:09:50 +0500 > Anas Jamshed <anasjamshed1994 at gmail.com> wrote: > > > I need help to these questions > > > > ### Question 1 > > Create a variable containing a sequence of numbers from 1 to 100: > > > > Iterate over the variables and print those numbers which are prime. > > > > > > ### Question 2 > > Create a matrix of size 3x3 called mat_1: > > > > Iterate over all the values one by one and print the element as > > well as the position in the matrix (row, col) > > You really should do your own homework. > > cheers, > > Rolf Turner > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > >[[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.