Skip to content

Instantly share code, notes, and snippets.

@maoxiaoke
Last active February 21, 2022 15:47
Show Gist options
  • Save maoxiaoke/660f545ed97fb44bc4dedee391b214ec to your computer and use it in GitHub Desktop.
Save maoxiaoke/660f545ed97fb44bc4dedee391b214ec to your computer and use it in GitHub Desktop.
__Algorithm__
/**
* Heap's algorithm
*/
function heapPermutation(arr,size,n){
var swap = function (index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
if (size === 1){
console.log(arr);
return;
}
for (let i = 0; i < size; i++){
heapPermutation(arr, size -1,n);
swap(size % 2 ? 0 : i, size - 1);
}
}
var arr = ['a','b','c','d'];
heapPermutation(arr,arr.length,arr.length);
var prime = function(num){
var i,j;
var arr = [];
for(i = 1; i < num; i++){
for(j=2; j < i; j++){
if(i%j === 0) {
break;
}
}
if(i <= j && i !=1){
arr.push(i);
}
}
return arr;
};
// 递归
function gcd(m ,n){
if(n) return gcd(n,m%n);
return m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment