Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

#MATL, (削除) 20 (削除ここまで) 17 bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2

#MATL, (削除) 20 (削除ここまで) 17 bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2

#MATL, (削除) 20 (削除ここまで) 17 bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2
edited body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 17 (削除) 20 (削除ここまで) 17 bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2

#MATL, 17 (削除) 20 (削除ここまで) bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2

#MATL, (削除) 20 (削除ここまで) 17 bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2
added 221 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 17 (削除) 20 (削除ここまで) bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2

#MATL, 17 (削除) 20 (削除ここまで) bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2

#MATL, 17 (削除) 20 (削除ここまで) bytes

This uses current version (5.0.0) of the language.

Approach based on @flawr's answer.

i:tg!X*t!Zd1=Y)Ym

Edit (April 28, 2015): Try it online! After this answer was posted, function Y) was renamed to X:; the link includes that change.

###Example

>> matl i:tg!X*t!Zd1=Y)Ym
> 100
0.6087

###Explanation

i: % vector 1, 2, ... up to input number
tg! % copy, convert into ones, transpose
X* % Kronecker product. Produces a matrix
t! % copy, transpose
Zd % gcd of all pairs
1= % is it equal to 1?
Y) % linearize into column array
Ym % compute mean

Old answer: 20 bytes

Oi:t"t@Zd1=sb+w]n2^/

Explanation

O % produce literal 0. Initiallizes count of co-prime pairs.
i % input number, say N
: % create vector 1, 2, ... N
t % duplicate of vector
" % for loop
 t % duplicate of vector
 @ % loop variable: each element from vector
 Zd % gcd of vector and loop variable. Produces a vector
 1=s % number of "1" in result. Each "1" indicates co-primality
 b+w % accumulate into count of co-prime pairs
] % end
n2^/ % divide by N^2
added 35 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading

AltStyle によって変換されたページ (->オリジナル) /