Skip to main content
thanks AlexA!
Source Link
plannapus
  • 9k
  • 22
  • 47

R, 4141 40 chars

a=scanb=min();b=mina<-scan(a));sum(5*b+(a-b)*c(2:4,1))

Usage:

> a=scanb=min();b=mina<-scan(a));sum(5*b+(a-b)*c(2:4,1))
1: 4 5 4 5
5: 
Read 4 items
[1] 84
> a=scanb=min();b=mina<-scan(a));sum(5*b+(a-b)*c(2:4,1))
1: 3 5 4 3
5: 
Read 4 items
[1] 70

In the last example, a is vector 3 5 4 3, a-b is 0 2 1 0, which we multiply with vector 2 3 4 1 thus giving 0 6 4 0 which we add with 5*b giving 15 21 19 15 (5*b being recycled for each member of the added vector, hence effectively adding 4*5*b), which we finally sum, thus giving 70.

R, 41 chars

a=scan();b=min(a);sum(5*b+(a-b)*c(2:4,1))

Usage:

> a=scan();b=min(a);sum(5*b+(a-b)*c(2:4,1))
1: 4 5 4 5
5: 
Read 4 items
[1] 84
> a=scan();b=min(a);sum(5*b+(a-b)*c(2:4,1))
1: 3 5 4 3
5: 
Read 4 items
[1] 70

In the last example, a is vector 3 5 4 3, a-b is 0 2 1 0, which we multiply with vector 2 3 4 1 thus giving 0 6 4 0 which we add with 5*b giving 15 21 19 15 (5*b being recycled for each member of the added vector, hence effectively adding 4*5*b), which we finally sum, thus giving 70.

R, 41 40 chars

b=min(a<-scan());sum(5*b+(a-b)*c(2:4,1))

Usage:

> b=min(a<-scan());sum(5*b+(a-b)*c(2:4,1))
1: 4 5 4 5
5: 
Read 4 items
[1] 84
> b=min(a<-scan());sum(5*b+(a-b)*c(2:4,1))
1: 3 5 4 3
5: 
Read 4 items
[1] 70

In the last example, a is vector 3 5 4 3, a-b is 0 2 1 0, which we multiply with vector 2 3 4 1 thus giving 0 6 4 0 which we add with 5*b giving 15 21 19 15 (5*b being recycled for each member of the added vector, hence effectively adding 4*5*b), which we finally sum, thus giving 70.

Source Link
plannapus
  • 9k
  • 22
  • 47

R, 41 chars

a=scan();b=min(a);sum(5*b+(a-b)*c(2:4,1))

Usage:

> a=scan();b=min(a);sum(5*b+(a-b)*c(2:4,1))
1: 4 5 4 5
5: 
Read 4 items
[1] 84
> a=scan();b=min(a);sum(5*b+(a-b)*c(2:4,1))
1: 3 5 4 3
5: 
Read 4 items
[1] 70

In the last example, a is vector 3 5 4 3, a-b is 0 2 1 0, which we multiply with vector 2 3 4 1 thus giving 0 6 4 0 which we add with 5*b giving 15 21 19 15 (5*b being recycled for each member of the added vector, hence effectively adding 4*5*b), which we finally sum, thus giving 70.