From e79e878400bd45c53c310e0e08d6233a472f90d0 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 8 Nov 2020 01:26:52 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-085/ulrich-rieke/cpp/ch-2.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-085/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-085/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include + +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( sqrt( static_cast( n ) ) ) ; + for ( int i = 2 ; i < lowerlimit + 1 ; i++ ) { + double expo = 2.0 ; + int power = static_cast(std::pow( static_cast( i ) , expo )) ; + while ( power < n ) { + expo += 1.0 ; + power = static_cast(std::pow( static_cast( i ) , expo )) ; + } + if ( power == n ) { + powerlimit = 1 ; + break ; + } + } + std::cout << powerlimit << std::endl ; + return 0 ; +} -- cgit