Category Archives: Python

Find sum of series 1+2+8+26+80+…….+n

we can not define 1+2+8+26+80+………. series in a simple manner to write  program.

But…..

2,8,26,80,242……………..can be define as below

The way to discover the next term is multiply the next number by 2 and then add it on.

i.e. the next number of 2 is 3

so, 3*2=6+2=8

9*2=18+8=26

27*2=54+26=80…….. and so on

            OR

Far more simply multiply the number by 3 and add 2

So,

2 * 3 + 2 = 8;

8 * 3 + 2 = 28

26 *3 + 2 = 80

80 *3 + 2 = 242. and so on

next_term=3*last_term+2

Now for finding the sum of the given series and add 1 with sum of this  series.

C language implementation

 

// C program to add 1+2+8+26+80+…….+n
//Author: Sani Kamal
//Date: 04-Oct-17

#include<stdio.h>

int main(){

int n,temp=0,i,sum=0,hold=0;
 printf("How many term");
 scanf("%d",&n);
 for(i=0;i<n-1;i++){
 temp =3*hold+2;
 hold=temp;
 sum +=hold;
 }
 printf("sum=%d\n",sum+1);
}

Java language implementation

import java.util.Scanner;

/**
 * Java program to add 1+2+8+26+80+.......+n
 *
 * @author Sani Kamal
 */
public class SeriesSum {

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 System.out.print("How many term?");
 int n = input.nextInt();
 int temp, hold = 0, sum = 0;
 for (int i = 0; i < n - 1; i++) {
 temp = 3 * hold + 2;
 hold = temp;
 sum += hold;

}
 System.out.println("Sum=" + (sum+1));

}

}

 

Python language implementation

# Python program to add 1+2+8+26+80+.......+n
# Author: Sani Kamal
# Date: 04-Oct-17

num = int(input("How many term?"))
hold = 0
sum=0
for i in range(0, num - 1):
    temp = 3 * hold + 2
    hold = temp
    sum += hold
print(sum + 1)
Advertisements

Polar Coordinates

Task
You are given a complex z. Your task is to convert it to polar coordinates.

Input Format

A single line containing the complex number z.

Output Format

Output two lines:
The first line should contain the value of r.
The second line should contain the value of Φ.

Sample Input

  1+2j

Sample Output

 2.23606797749979 
 1.1071487177940904


import cmath
complex_number=complex(input())
print(abs(complex_number))
print(cmath.phase(complex_number))

Set .add()

Task

Apply your knowledge of the .add() operation to help your friend Rupal.

Rupal has a huge collection of country stamps. She decided to count the total number of distinct country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack of N country stamps.

Find the total number of distinct country stamps.

Input Format

The first line contains an integer N, the total number of country stamps.
The next N lines contains the name of the country where the stamp is from.

 

num = int(input())
country_name = set()
for i in range(num):
    country_name.add(input())
print(len(country_name))

List Comprehensions

Let’s learn about list comprehensions! You are given three integers X,Y and Z representing the dimensions of a cuboid along with an integer N. You have to print a list of all possible coordinates given by  (i,j,k) on a 3D grid where the sum of  i+j+k  is not equal to N . Here, 0<=i<=X;0<=j<=Y;0<=k<=Z

Input Format

Four integers  X,Y,Z and N  each on four separate lines, respectively.

Constraints

Print the list in lexicographic increasing order.

x,y,z,n=[int(input())for i in range(4)]
p=[[a,b,c]for a in range(x+1)for b in range(y+1)for c in range(z+1) if a+b+c!=n]
print(p)