logo
down
shadow

Finding numbers who digits ^ (digit position) == original number i.e. (89 = 8^1 + 9^2), but the code won't return anythi


Finding numbers who digits ^ (digit position) == original number i.e. (89 = 8^1 + 9^2), but the code won't return anythi

Content Index :

Finding numbers who digits ^ (digit position) == original number i.e. (89 = 8^1 + 9^2), but the code won't return anythi
Tag : python , By : Adam Hill
Date : December 05 2020, 12:18 PM

will be helpful for those in need I think, your code doesn't work, because the missing solution 1676 contains two times the 6. So str(i).index(x) will return the wrong index for the second 6 which makes the calculation faulty.
I guess, this is one of the times, where I'd start without list comprehensions and first get the code to work. Is this generally the result you want?
def sum_dig_pow(a, b): 
    result = []
    for number in range(a, b+1):
        calculation = 0
        for position, digit in enumerate(map(int, str(number))):
            calculation += digit**(position + 1)
        if calculation == number:
            result += [number]
    return result

print(sum_dig_pow(1, 2000))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676]
def sum_dig_pow(a, b): 
    result = []
    for number in range(a, b+1):
        calculation = sum([digit**(position + 1) for position, digit in enumerate(map(int, str(number)))])
        if calculation == number:
            result += [number]
    return result
def sum_dig_pow(a, b): 
    return [number for number in range(a, b+1) if sum([digit**(position + 1) for position, digit in enumerate(map(int, str(number)))]) == number]
sum_dig_pow = lambda a, b: [number for number in range(a, b+1) if sum([digit**(position + 1) for position, digit in enumerate(map(int, str(number)))]) == number]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Finding if two numbers have the same digit and then remove them from in the original number in Haskell


Tag : haskell , By : alchemist
Date : March 29 2020, 07:55 AM
Does that help Let x and y be the two numbers. Then one can remove the digits in x which it has in common with y like this:
Prelude> import Data.List
Prelude Data.List> let x = 68
Prelude Data.List> let y = 76
Prelude Data.List> read (show x \\ show y) :: Int
8
s <- nub $ intersect (show x) (show y)
map (read . delete s . show) [x, y]

Code for finding the greatest product of five consecutive digits in the 1000-digit number


Tag : java , By : micaleel
Date : March 29 2020, 07:55 AM
will be helpful for those in need charAt() reports 48 for '0' and 49 for '1' etc. as it is the character values.
Subtract 48 from each and try again.

how to get number of possible 4 digit numbers with constraints on possible digits and position of digits


Tag : math , By : user123585
Date : March 29 2020, 07:55 AM
this one helps. So our hint is: n1n2n3n4, use all of 1234 exactly once.
1) There are three places we can put 1 in, leaving us with _1n3n4, _n21n4 and _n2n31.
_1__
2143
4123
3142

__1_
3412
4312
2413

___1
4321
3421
2341

Find the a 4 digit number who's square is 8 digits AND last 4 digits are the original number


Tag : python , By : Alex S
Date : March 29 2020, 07:55 AM
I wish this helpful for you From the comments on my answer here, the question was asked (paraphrase): , Here is a 1-liner solution without any modules:
>>> next((x for x in range(1000, 10000) if str(x*x)[-4:] == str(x)), None)
9376
>>> next((x for x in range(3163, 10000) if str(x*x)[-4:] == str(x)), None)
9376

Code for finding largest product of 13 consecutive digits of a 1000 digit number does not give required output


Tag : python , By : kiirpi
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The code is to find the largest possible product of 13 consecutive digits of a 1000 digit number. When I tried to run it on IDLE, it just gave RESTART and the directory where I saved the .py file. When I tried this on Pycharm(I know its not the IDE's problem, but I just had to try), no output. Am I doing something wrong? , Your original code can be simplified to the following.

n=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

def product(j):
    p = 1
    for i in range(j,j+13):
        p=p*s[i]
    return(p)
s=[]
for i in range(0,1000):
    s=s+[n%10]
    n=n//10
k = 0
N = len(s)
j = 0
while j<N-13:  #need to 13 elements before end
    c = product(j)
    if c>k:
        k=c
    j = j + 1
print(k)
n=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

def product(j):
    p = 1
    for i in range(j,j+13):
        p=p*s[i]
    return(p)
s=[]
for i in range(0,1000):
    s=s+[n%10]
    n=n//10
k = 0
N = len(s)
j = 0
while j<N-13:  #need to 13 elements before end
    c = product(j)
    if c>k:
        k=c
    j = j + 1
print(k)

digits = str(n) # generates digits of number
products = [product(j) for j in range(1000-13)] # list comprehension
                                                # uses you product function
print(max(products))
digits = str(n) # generates digits of number
products = [product(j) for j in range(1000-13)] # list comprehension
                                                # uses you product function
print(max(products))
Related Posts Related QUESTIONS :
  • My css and images arent showing in django
  • Probability mass function sum 2 dice roll?
  • Cannot call ubuntu 'ulimit' from python subprocess without using shell option
  • Dataframe Timestamp Filter for new/repeating value
  • Problem with clicking select2 dropdownlist in selenium
  • pandas dataframe masks to write values into new column
  • How to click on item in navigation bar on top of page using selenium python?
  • Add multiple EntityRuler with spaCy (ValueError: 'entity_ruler' already exists in pipeline)
  • error when replacing missing ')' using negative look ahead regex in python
  • Is there a way to remove specific strings from indexes using a for loop?
  • select multiple tags by position in beautifulSoup
  • pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout
  • Shipping PyGObject/GTK+ app on Windows with MingW
  • Python script to deduplicate lines in multiple files
  • How to prevent window and widgets in a pyqt5 application from changing size when the visibility of one widget is altered
  • How to draw stacked bar plot from df.groupby('feature')['label'].value_counts()
  • Python subprocess doesn't work without sleep
  • How can I adjust 'the time' in python with module Re
  • Join original np array with resulting np array in a form of dictionary? multidimensional array? etc?
  • Forcing labels on histograms in each individual graph in a figure
  • For an infinite dataset, is the data used in each epoch the same?
  • Is there a more efficent way to extend a string?
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com