4

I'm having a problem with my code

def bconver(n,b):
    for i in range(n):
        x = b ** i

I am trying to have b to the power of all the i values from the range of n. I need to be able to take those values and divide them by n allowing me to change it to any base.

I have tried this code but it doesn't work since I can't divide those values after. Also, I need the answers not in a list

x = [b ** i for i in range(n)]  
  • no my question is very different, Im only asking how to take all the values of `i ` and put them to the power of b. no base conversion question was asked –  Nov 20 '17 at 03:40
  • Fair enough, then I did not understand your comment on my answer. – Reblochon Masque Nov 20 '17 at 03:45

2 Answers2

2

You can divide each value in your list comprehension by n:

x = [b ** i for i in range(n)]  
x_over_n = [a/n for a in x]

edit:

def x_to_the_i_over_n(x, i, n):
    return x**i / n

n = 4
x = 123
results = []
for i in range(n):
    results.append(x_to_the_i_over_n(x, i, n))

edit 2:

import numpy

b = numpy.array(range(10))
b**2
#--> array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

b**2 / 4
#--> array([  0.  ,   0.25,   1.  ,   2.25,   4.  ,   6.25,   9.  ,  12.25, 16.  ,  20.25])
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Oh wow I overlook that thanks you, but I need it without a list –  Nov 20 '17 at 03:21
  • How exactly do you need it? – Reblochon Masque Nov 20 '17 at 03:22
  • the overall problem I am trying to solve is to change a base 10 number from to any base, without the abc thing. The final answer I need to get is a number not in a list –  Nov 20 '17 at 03:24
  • I just need to be able to do the exponential notation without a list –  Nov 20 '17 at 03:45
  • I am sorry, I don't understand what "without the abc" means? – Reblochon Masque Nov 20 '17 at 03:45
  • Sorry for the clarification issue, the abc thing I am referring to is when people do the base conversion they do x =123456789ABCD, but that was just for info on the whole problem. The question I am trying to ask is how do I do the exponential notation not in a list just to be able to get single integers –  Nov 20 '17 at 03:48
  • I edited my answer, but I am still rather unclear what exactly you want – Reblochon Masque Nov 20 '17 at 03:51
  • to make it more clear, ignore the base conversion part. what I want is to be able to do this, example (x= for I in range 5) take all these value b^x so 4^1, 4^2, 4^3 and so on without a list, the function takes in only two parameters. –  Nov 20 '17 at 03:56
  • Still unclear to me... maybe I have a blind spot here. Edited again; is that what you want? (with numpy, you can do all ops at once, but the results are still in an array. – Reblochon Masque Nov 20 '17 at 04:01
  • I am very very sorry, but without any libraries. And ya I can't have an array only integers. –  Nov 20 '17 at 04:02
  • Can you post the output you need, maybe that would clarify it. – Reblochon Masque Nov 20 '17 at 04:04
  • yes maybe this will help, some pseudocode. the function with two parameters(n,b) if 5 were to be inputted as n. and 14 for b. This would happen, 14^0, 14^1,14^2,14^3,14^4,14^5. the output would be 1 14 196 2744 38416 and 537824 no list and then I can divide those single integers by n if I need –  Nov 20 '17 at 04:10
  • I see... you can only return one object from a function, I am afraid; in this case, either one integer, or a collection of integers (list, tuple, or other data structure) – Reblochon Masque Nov 20 '17 at 04:20
  • oh ok I see, I don't need those values all printed out as the goal for me is to have an integer base 10 changed to any base. could you do it so the numbers run through it but the values don't get printed out. I can finish the rest I just need this part. –  Nov 20 '17 at 04:34
  • I modified the first edit so the results are stored in a list i/o printed. The first list comprehension also works. – Reblochon Masque Nov 20 '17 at 04:53
  • Thanks, can’t beleve a such a high class guy like you answerd my question –  Nov 20 '17 at 04:55
1

You can perform any operations you want inside that list comprehension. For example,

x = [(b ** i) / n for i in range(n)]

sounds like the final result you're looking for. If you want the intermediate result, just use another list comprehension:

x = [b ** i for i in range(n)]
x_divided_by_n = [element / n for element in x]

If you want more compact ways to express these kinds of operations, look into using numpy.

Josh Karpel
  • 2,110
  • 2
  • 10
  • 21
  • How would I go about using modulo for x and n. From the first example you gave –  Nov 20 '17 at 03:29
  • @nick The standard modulo operator `%` works in Python (`5 % 3 = 2`, for example). – Josh Karpel Nov 20 '17 at 03:31
  • yes I understand that but after doing x = [(b ** i) / n for i in range(n)] how would I do for example p = n%x –  Nov 20 '17 at 03:35
  • also im not sure if this changes the code but I need the answer not in a list –  Nov 20 '17 at 03:37