0% found this document useful (0 votes)
2 views2 pages

R 4.5.1 Fibonacci and Primes Code

The document details the usage of R version 4.5.1, including basic commands and functions for mathematical operations such as generating Fibonacci numbers and finding prime numbers using the Sieve of Eratosthenes. It also highlights error messages encountered during coding attempts and the corrections made. The document serves as a practical guide for users to understand R programming syntax and functionality.

Uploaded by

linhptk23413e
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

R 4.5.1 Fibonacci and Primes Code

The document details the usage of R version 4.5.1, including basic commands and functions for mathematical operations such as generating Fibonacci numbers and finding prime numbers using the Sieve of Eratosthenes. It also highlights error messages encountered during coding attempts and the corrections made. The document serves as a practical guide for users to understand R programming syntax and functionality.

Uploaded by

linhptk23413e
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R version 4.5.

1 (2025-06-13) -- "Great Square Root"


Copyright (C) 2025 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin20

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'[Link]()' for an HTML browser interface to help.
Type 'q()' to quit R.

[[Link] GUI 1.82 (8536) aarch64-apple-darwin20]

[Workspace restored from /Users/linhymn/.RData]


[History restored from /Users/linhymn/.[Link]]

> #Fibonacci Un = Un-1 + U n -2, U1 =1 U2=1


> Fib <- numeric(12)
> Fib
[1] 0 0 0 0 0 0 0 0 0 0 0 0
> Fib[1] <- Fib[2] <- 1
> Fib
[1] 1 1 0 0 0 0 0 0 0 0 0 0
> for (i in 3:12) []
Error: unexpected '[' in "for (i in 3:12) ["
> for (i in 3:12) {Fib[i] <- Fib[i-2] + Fib[i-1] }
> Fib
[1] 1 1 2 3 5 8 13 21 34 55 89 144
> x <- 3
> if (x>2) {y <- 2*x}
> else {y <- 3*x}
Error: unexpected 'else' in "else"
> if (x>2) {y <- 2*x} else {y <- 3*x}
> y
[1] 6
> if (x>2) {y <- 2*x} else {y <- 3*x}
> corplot <- function(x, y, plotit) {if (plotit == TRUE) plot(x, y) cor(x, y)}
Error: unexpected symbol in "corplot <- function(x, y, plotit) {if (plotit == TRUE) plot(x, y) cor"
> corplot <- function(x, y, plotit) {
+ if (plotit == TRUE) plot(x, y)
+ cor(x, y)
+ }
> corplot(c(2, 5, 7), c(5, 6, 8), FALSE)
[1] 0.953821
> # Example: Sieve of Eratosthenes: all primes up to n
> Primesupto,n <- function(n){}
Error: unexpected ',' in "Primesupto,"
> Primesupto,n <- function(n){
Error: unexpected ',' in "Primesupto,"
> if (n >= 2) {
+ sieve <- seq(2, n)
+ primes <- c()
+ for (i in seq(2, n)) {
+ if (any(sieve == i)) {
+ primes <- c(primes, i)
+ sieve <- sieve[(sieve %% i) != 0]
+ }
+ }
+ return(primes)
+ } else {
+ stop("Input value of n should be at least 2.")
+ }
Error: object 'n' not found
> }
Error: unexpected '}' in "}"
> Eratosthenes <- function(n) {
+ if (n >= 2) {
+ sieve <- seq(2, n)
+ primes <- c()
+ for (i in seq(2, n)) {
+ if (any(sieve == i)) {
+ primes <- c(primes, i)
+ sieve <- sieve[(sieve %% i) != 0]
+ }
+ }
+ return(primes)
+ } else {
+ stop("Input value of n should be at least 2.")
+ }
+ }
> Eratosthenes(50)
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
> Fib1 <- 1
> Fib2 <- 1
> Fibonacci <- c(Fib1, Fib2)
> Fib
[1] 1 1 2 3 5 8 13 21 34 55 89 144
> while (Fib2 < 300) {
+ + Fib <- c(Fib, Fib2)
+ + oldFib2 <- Fib2
+ + Fib2 <- Fib1 + Fib2
+ + Fib1 <- oldFib2
+ + }
Error: unexpected '}' in:
"+ Fib1 <- oldFib2
+ }"
> while (Fib2 < 300) {
+ Fibonacci <- c(Fibonacci, Fib2)
+ oldFib2 <- Fib2
+ Fib2 <- Fib1 + Fib2
+ Fib1 <- oldFib2
+ }
> Fib
[1] 1 1 2 3 5 8 13 21 34 55 89 144
> f <- x^3 + 2*x^2 - 7
> tolerance <- 1e-6
>

You might also like