Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
GET ALL PRIME FACTORS OF A NUMBER
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//g++ 5.4.0 //get all PRIME FACTORS OF A NUMBER #include <bits/stdc++.h> using namespace std; vector<int> simpleSieve(int limit){ bool isPrime[limit+1]; memset(isPrime, true, sizeof(isPrime)); for(int i=2;i*i<=limit;i++){ if(isPrime[i]){ for(int j=i*i;j<=limit;j+=i){ isPrime[j] = false; } } } vector<int> primes; for(int i=2;i<=limit;++i){ if(isPrime[i]) primes.push_back(i); } return primes; } vector<int> getPrimeFactors(int n){ int limit = sqrt(n); vector<int> primeFactors; vector<int> primes = simpleSieve(limit); for(auto i: primes){ while(n%i==0){ primeFactors.push_back(i); n/=i; } } if(n>1) primeFactors.push_back(n); return primeFactors; } int main() { int n; cin>>n; vector<int> primeFactors = getPrimeFactors(n); for(auto i: primeFactors) cout<<i<<", "; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion