diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-11-08 01:26:52 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-11-08 01:26:52 +0000 |
| commit | e79e878400bd45c53c310e0e08d6233a472f90d0 (patch) | |
| tree | ad88771c87e834bbe956ac767a73a24bbcb0bb04 /challenge-085/ulrich-rieke/cpp/ch-2.cpp | |
| parent | 850b36dc466f9249938e1ef6fefef477d1d4b672 (diff) | |
| download | perlweeklychallenge-club-e79e878400bd45c53c310e0e08d6233a472f90d0.tar.gz perlweeklychallenge-club-e79e878400bd45c53c310e0e08d6233a472f90d0.tar.bz2 perlweeklychallenge-club-e79e878400bd45c53c310e0e08d6233a472f90d0.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-085/ulrich-rieke/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-085/ulrich-rieke/cpp/ch-2.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-085/ulrich-rieke/cpp/ch-2.cpp b/challenge-085/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..ed72f314bb --- /dev/null +++ b/challenge-085/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <cmath> +#include <cstdlib> + +int main( int argc, char * argv[] ) { + int n = std::atoi( argv[ 1 ] ) ; + int powerlimit = 0 ; + //the limit is the square root of n. We cycle through all numbers from 2 to + //the square root and finish cycling as soon as we find 2 integers that + //form n ; the functions from cmath require some type juggling + int lowerlimit = static_cast<int>( sqrt( static_cast<double>( n ) ) ) ; + for ( int i = 2 ; i < lowerlimit + 1 ; i++ ) { + double expo = 2.0 ; + int power = static_cast<int>(std::pow( static_cast<double>( i ) , expo )) ; + while ( power < n ) { + expo += 1.0 ; + power = static_cast<int>(std::pow( static_cast<double>( i ) , expo )) ; + } + if ( power == n ) { + powerlimit = 1 ; + break ; + } + } + std::cout << powerlimit << std::endl ; + return 0 ; +} |
