6 kyu

Playing with digits

3,732 of 144,389g964

Description:

Some numbers have funny properties. For example:

  • 89 --> 8¹ + 9² = 89 * 1
  • 695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
  • 46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

Given two positive integers n and p, we want to find a positive integer k, if it exists, such that the sum of the digits of n raised to consecutive powers starting from p is equal to k * n.

In other words, writing the consecutive digits of n as a, b, c, d ..., is there an integer k such that :

(ap+bp+1+cp+2+dp+3+...)=nk(a^p + b^{p + 1} + c^{p + 2} + d^{p + 3} + ...) = n * k

If it is the case we will return k, if not return -1.

Note: n and p will always be strictly positive integers.

Examples:

n = 89; p = 1 ---> 1 since 8¹ + 9² = 89 = 89 * 1

n = 92; p = 1 ---> -1 since there is no k such that 9¹ + 2² equals 92 * k

n = 695; p = 2 ---> 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2

n = 46288; p = 3 ---> 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Fundamentals
Mathematics

More By Author:

Check out these other kata created by g964

Stats:

CreatedMay 12, 2015
PublishedMay 12, 2015
Warriors Trained355277
Total Skips74421
Total Code Submissions513090
Total Times Completed144389
Ruby Completions3732
Python Completions55418
C# Completions7975
Java Completions13898
JavaScript Completions43249
Haskell Completions1120
Clojure Completions284
CoffeeScript Completions89
C++ Completions6102
PHP Completions3105
Crystal Completions37
F# Completions109
Swift Completions864
C Completions3010
TypeScript Completions2480
Go Completions1811
Shell Completions167
OCaml Completions59
Julia Completions116
Scala Completions402
R Completions199
PowerShell Completions83
Kotlin Completions927
NASM Completions23
Nim Completions29
Rust Completions1400
Racket Completions32
Reason Completions8
Lua Completions153
CommonLisp Completions104
Pascal Completions22
Perl Completions26
Raku Completions7
Elm Completions12
D Completions9
COBOL Completions11
Prolog Completions13
Factor Completions10
Total Stars3589
% of votes with a positive feedback rating90% of 13635
Total "Very Satisfied" Votes11216
Total "Somewhat Satisfied" Votes2191
Total "Not Satisfied" Votes228
Ad
Contributors
  • g964 Avatar
  • jhoffner Avatar
  • ZozoFouchtra Avatar
  • naithar Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Voile Avatar
  • kdmatrosov Avatar
  • lbvf50mobile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • uniapi Avatar
  • trashy_incel Avatar
  • akar-0 Avatar
  • Kacarott Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • razetime Avatar
  • KayleighWasTaken Avatar
  • PetitLu117 Avatar
Ad