Toggle navigation
Golibrary Code Playground - Everyone for education
Run
(current)
Fork
Settings
Share
0
426
Anonymous
HTML/CSS/Javascript
Python
Ruby
Clojure
PHP
Javascript (NodeJS)
Scala
Go
C/C++
Java
VB.Net
C#
Bash
Objective C
MySQL
Perl
Rust
HTML/CSS/Javascript
Search
Login
Login
Reset Password ?!
Login
Register
×
RESET PASSWORD
@
×
Allowed Extensions
Only this extensions are allowed: jpg,png,gif,jpeg
×
Error Message
×
Share this code
Url of this code
Send SMS with url of this code
Save As Github Gist
×
Github Gist
×
SETTINGS
HTML
CSS
Javascript
Title
Description
Meta Tags
Add meta tag example
Known ressources
--
Foundation
Bootstrap
CSS Preprocessor
CSS
LESS
SCSS
External CSS
Add another ressource
Known ressources
--
JQuery UI
Modernizr
Tree.js
JQuery
External JS
Add another ressource
CSS
→
/* CSS */ background: #f9f9f9; height: 100%;
Full Screen
Javascript
→
// first we find all prime factors and using the prime factors, determine all non prime factors window.onload = init; function init() { var allFactors = solution(1e+21); document.write('All factors of 1e+21 are: '+allFactors); } // algorithm to calculate all Factors of very large number function solution(N) { var output = []; findPrimeFactors(N, output); var uniquesObj = countUniques(output); var arr1 = [], arr2 = [], result = []; for (var key in uniquesObj) { arr1.push((key)); arr2.push(uniquesObj[key]); } findFactors(arr1, arr2, 0, 1, result ); return (N === 0 || N ===1) ? [N] : result.sort((a,b) => a-b); } function countUniques(arr) { return arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {}) } function findFactors(primeDivisors, multiplicity, currentDivisor, currentResult, output) { if (currentDivisor == primeDivisors.length) { output.push(currentResult); return; } for (var i = 0; i <= multiplicity[currentDivisor]; ++i) { findFactors(primeDivisors, multiplicity, currentDivisor + 1, currentResult, output); currentResult *= primeDivisors[currentDivisor]; } } function calcProducts(arr) { var product = 1; for (var t = 0; t < arr.length; t++) product *= arr[t]; return ; } // checks if the given number is prime function isPrime(number, getIndex = false) { for (var t = 2; t <= Math.round(Math.sqrt(number)); t++) { if (number % t === 0) { if (getIndex) return t; return false; } } return true; } // find prime factors of given number. Returns an array of prime factors of number function findPrimeFactors(num, factors = []) { if (isPrime(num)) { factors.push(num); return factors; } // check if number if even if (num % 2 === 0) { // check if number is even and push even factor in output array factors.push(2); findPrimeFactors(parseInt(num / 2), factors); // recursive call. Continue until all prime factors are found } else { // check if number is odd or prime and push prime/odd factor in output array var j = isPrime(num, true); if (j >= 2) { factors.push(j); } findPrimeFactors(parseInt(num / j), factors); // recursive call. Continue until all prime factors are found } }
HTML
→
<!-- HTML --> <b>Start new code !</b>