6 Replies - 877 Views - Last Post: 22 March 2016 - 03:44 AM Rate Topic: -----

#1 macbob   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 06-March 16

problem based on building Leibniz harmonic triangle

Posted 20 March 2016 - 12:04 PM

The problem: Leibniz harmonic triangle
Write a program that prints the harmonic triangle for the depth n, where n is an input value.
Inspiration source↓
leibniz harmonic triangle

In the leibniz function, i am stuck at fractions with the numbers
What i have done
import fractions
import math


def nCk(n, k):  # combinations n choose k
    f = math.factorial
    return int(f(n) / f(k) / f(n - k))
def pascaltr(n):
    depth = []
    for i in range(n):
        row = []
        for j in range(i + 1):
            row.append(nCk(i, j))
        depth.append(row)
    return depth
def leibniz(n):
    l = len(lists)
    j=1
    for i in range(l):
        k=len(lists[i])+1
        while j==k:
            lists[i]=fractions.Fraction(1,j*lists[i][j-1])
            j+=1
        print(lists[i])
    print(lists)

n = eval(input("Input the depth of Harmonic triangle:"))
lists = pascaltr(n)
print(pascaltr(n))
leibniz(n)

I know the last function on leibniz function isn't thought well ~~my brain is exploding
Please help

Is This A Good Question/Topic? 0
  • +

Replies To: problem based on building Leibniz harmonic triangle

#2 macbob   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 06-March 16

Re: problem based on building Leibniz harmonic triangle

Posted 20 March 2016 - 12:42 PM

def leibniz(n):
    l = len(lists)
    j = 1
    for i in range(l):
        k = len(lists[i])#Fixed here
        while j == k:
            lists[i][j-1] = fractions.Fraction(1, j * lists[i][j-1])#fixed here
            j += 1
        print(lists[i])
    print(lists)

output for depth 3

Input the depth of Harmonic triangle:3
[Fraction(1, 1)]
[1, Fraction(1, 2)]
[1, 2, Fraction(1, 3)]
[[Fraction(1, 1)], [1, Fraction(1, 2)], [1, 2, Fraction(1, 3)]]

please guide what am i doing wrong
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,428
  • Joined: 27-December 13

Re: problem based on building Leibniz harmonic triangle

Posted 20 March 2016 - 01:08 PM

I have never heard about this triangle before, but I think you make things very complicated.
Normally we don't 'throw' code here, but with this simple code I make an exception:
n = eval(input("Input the depth of Harmonic triangle:"))

lists = []

for i in range(2, n+2):  # this loop generates first number in each row
    lists.append([i])
lists[0].append(2)  # first row is treated specially

for i in range(1, n):
    for j in range(i):
        a = lists[i-1][j]
        b = lists[i][j]
        lists[i].append(a*b//(b-a))
    lists[i].append(lists[i][0])  # last value in each row added
print(lists)
The code generates the denominators, but as all numerators are one, this should be ok. The very first point (value 1) is omitted here.
Was This Post Helpful? 1
  • +
  • -

#4 macbob   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 06-March 16

Re: problem based on building Leibniz harmonic triangle

Posted 20 March 2016 - 01:35 PM

i know what you mean but i got that answer with value 1 in the pascaltr(n)
Also this whole program algorithm was based on making the pascal triangle then convert to leibniz triangle so the only thing remained was to make the fractions

After another try i found out
def leibniz(n):
    l = len(lists)
    j = 1
    for i in range(l):
        k = len(lists[i])+1
        print("-")
        while j != k:
            print("--")
            lists[i][j-len(lists[i])] = fractions.Fraction(1, j * lists[i][j-len(lists[i])])#fixed here
            j += 1
        print(lists[i])

where the output was
[Fraction(1, 1)]
[Fraction(1, 2), 1]
[Fraction(1, 3), 2, 1]
but with a different form again not all fractioned
where i need only to fractionise all the numbers so it would be
1/1
1/2 , 1/2
1/3, 1/6, 1/3

print("-")
print("--") were to see how the loop works they they have nothing to do here
Was This Post Helpful? 0
  • +
  • -

#5 macbob   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 06-March 16

Re: problem based on building Leibniz harmonic triangle

Posted 20 March 2016 - 01:56 PM

actually i am sorry it seems i didn't understood you
Was This Post Helpful? 0
  • +
  • -

#6 macbob   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 06-March 16

Re: problem based on building Leibniz harmonic triangle

Posted 20 March 2016 - 03:10 PM

well in the end i figured out
this is the last function
def leibniz(n):
    l = len(lists)
    for i in range(l):
        for j in range(len(lists[i])):
            if j==0:
               lists[i][j] = fractions.Fraction(1, lists[i][j] * len(lists[i]))
            else:
                lists[i][j] = fractions.Fraction(1, lists[i][j] * len(lists[i]))
        print(lists[i])

sorry for taking your time

ops this
def leibniz(n):
    l = len(lists)
    for i in range(l):
        for j in range(len(lists[i])):
                lists[i][j] = fractions.Fraction(1, lists[i][j] * len(lists[i]))
        print(lists[i])

Was This Post Helpful? 0
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,428
  • Joined: 27-December 13

Re: problem based on building Leibniz harmonic triangle

Posted 22 March 2016 - 03:44 AM

Sorry for my late answer - I've been on a small travel.
You now have a working code! Good! This is the primary goal for all programmers.
Then you can - if time and energy allows - 'polish'/fine-tune/optimize the code.
Optimizations often can be further optimized etc.
The important thing is that you'll often learn quite a lot from the optimization effort. I do.

I find the code below more Pythonic:
def leibniz(n):
    for row in lists:
        lgt = len(row)
        for j, value in enumerate(row):
            row[j] = fractions.Fraction(1, value * lgt)
        print(row)
But even this code bothers me because it changes the item iterated over, i.e. I iterate over row (in line 4) and change row[j] (in line 5).
In this case it is ok, but often such constructions can lead to failure.

Maybe some of the more experienced guys will comment..?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1