python simple basic program on geeksforgeeks - python

Can anyone help me with the solution of this ?
Given a number N, print all the composite numbers less than or equal to N. The number should be printed in ascending order.
Input:
The first line contain an Integer T denoting the number of test cases . Then T test cases follow. Each test case consist of an single integer N.
Output:
Print all the composite Number form 0 to N.
Constraints:
1 ≤ T ≤ 50
4 ≤ N ≤ 10000
Example:
Input:
2
10
6
Output:
4 6 8 9 10
4 6
My solution is as below :
def comp(n):
for i in range (4,n+1):
for j in range(2,i):
if i % j == 0 :
print(i)
break
t = int(input(""))
while(t >=1 & t <= 50):
for k in range(0,t):
p = int(input(""))
if(p >=4 & p <= 10000):
comp(p)
but giving EOFError on p = int(input(""))

This error occurs when you are providing lesser input values than required. Your program is expecting more input's than you actually provided.
if you are using a pipe to send data to stdin of the script then make sure each input is sent in a newline as shown below.
time echo -e "2\n'3 4 5 6 7'\n3" | python sample.py

The culprit here is the while loop instead of if. As, the t is a constant value, the while loop will never end and thus, p = int(input("")) will get executed more than the required times. Hence, EOFError on p = int(input("")).

A simple solution using recursion.
def check_prime(n):
for i in range(2,int(math.sqrt(n))+1):
if(n%i==0):
return 0
return 1
def print_composite(n):
if(n<5):
print(n),
return
print_composite(n-1)
if(check_prime(n)==0):
print(n),
return
Sample outputs from my terminal
>>> print_composite(10)
4 6 8 9 10
>>> print_composite(6)
4 6
>>> print_composite(16)
4 6 8 9 10 12 14 15 16

Related

line 1, in <module> F,S = list(map(int,input().split())) ValueError: not enough values to unpack (expected 2, got 1)

i am getting errors about listing
i think its because of some inputs have only one integer
i tried seperating them with map split but it didnt work out
Question:
Enter two integers then compare their values.
The first line of the input consist of a single integer number t which determines the number of tests.
In each of next t lines there are two numbers m and n that are separated by a space character.
sample input:
Input Format:
5
9 2
-3 -5
5 28
0 0
19 13
sample output:
9 is greater than 2
-3 is greater than -5
5 is smaller than 28
n is equal m: 0
19 is greater than 13
my code:
n,m = list(map(int,input().split()))
int(n)
int(m)
if n > m:
print(n+('is greater than'+m))
if m < n:
print(m+('is smaller than'+n))
if m > n:
print(m+('is greater than'+n))
if n < m:
print(n+('is smaller than'+m))
if n == m:
print(n+('is equal'+m))
This erros is happening because you try to separate n and m in the first input.
However, the first input should be a loop control bariable.
An example of the code would be:
control = int(input("Control variable: "))
for x in range(control):
n,m = list(map(int, input().split()))
if n > m:
print(f'{n} is grater than {m}')
elif m > n:
print(f'{m} is grater than {n}')
else:
print(f'{n} is equal to {m}')
x += 1
after, you can perform a check if the user actually passed two numbers.

Loop while basics in python 3

I am reading a python basics book and there is one function which I don't understand how it works. How is is possible that output looks like pow function even there are not any ** or pow operation? Would be nice if anyone can help because I am getting more and more frustrated
loop while
summary = 1
number = 1
while number <= 6:
i = 1
p = number
while i < 5:
p *= number
i += 1
print(number, "to 5", p)
summary += p
number += 1
print("sum of fifth powers of numbers from 1 to 6 is", summary)
output
1 to 5 1
2 to 5 32
3 to 5 243
4 to 5 1024
5 to 5 3125
6 to 5 7776
sum of fifth powers of numbers from 1 to 6 is 12202
Let me explain this code briefly,
first we are defining,
> summary = 1
> number = 1
Here we are defining and initialising the two variables summary and number.
> while number <= 6:
> i = 1
> p = number
In above code we are starting a while loop which will run while the value of number variable is less than or equal to 6. So, the loop will run from 1 to 6. we are taking a variable i = 1 and p = number here.
> while i < 5:
> p *= number
> i += 1
> print(number, "to 5", p)
> summary += p
> number += 1
> print("sum of fifth powers of numbers from 1 to 6 is", summary)
Now, we are having an another nested while loop and this will run for the values 1 to 4 of i variable. As we can see in the loop, the variable p will be multiplied with itself for 4 times so we will get the 5th power of the particular number. then we are increasing value of number by 1 and adding the value of 5th power in variable summary and lastly we are printing that.
Let me explain with an example
when number=2 (i.e after finding fifth power of 1)
value of p=2 and i=1
then inner loop i.e
while i<5 :
p* = number //i.e p = p*number
i+= 1 //i.e i=i+1
goes like this,
iteration 1: p= 2*2 i.e p=4
i=1+1 i.e i=2 which is less than 5
iteration 2: p= 4*2 i.e p=8
i=2+1 i.e i=3 which is less than 5
iteration 3: p= 8*2 i.e p=16
i=3+1 i.e i=4 which is less than 5
iteration 4: p= 16*2 i.e p=32
i=4+1 i.e i=5 which is equal to 5, so it comes out of loop
therefore, 2 to 5=32
this is how we get fifth power of a number

Is there a more effective implementation?

Problem statement:
You are given two positive integers
d
and
s
. Find minimal positive integer
n
which is divisible by
d
and has sum of digits equal to
s
.
Input:
The first line contains two positive integers
d
and
s
(
1
≤
d
≤
500
,
1
≤
s
≤
5000
) separated by space.
Output:
Print the required number or -1 if it doesn't exist.
This my code:
d_and_s = [int(x) for x in input().split()]
counter_dracula = 0
while True:
if counter_dracula%d_and_s[0] == 0 and sum(map(int, str(counter_dracula))) == d_and_s[1]:
break
counter_dracula += 1
print(counter_dracula)
That's my implementation but clearly there must be a faster way.
For example if the Input is 13 and 50 the output is 699998.
My code gives me the correct answer but takes a long time but even ultra longer in this sample testcase: Input is 61 and 2 and the output is 1000000000000000000000000000001.
How can I implement them correctly using Python 3?
(yey, the question opened again :-) )
For one quite improvement, realize that the numbers divisible by d is d, 2*d, 3*d, 4*d , ...
So instead of incrementing the loop by 1 every time, you can increment with d
def sum_digits(n):
r = 0
while n:
r, n = r + n % 10, n // 10
return r
d, s = [int(x) for x in input().split()]
counter = 0
while True:
counter += d
if sum_digits(counter) == s:
break
print(counter)

Implement the recursion in pythonic way

I have a recursion:
C(n) = min{
C(n/3) + 1 if n ⋮ 3,
C(n/2) + 1 if n ⋮ 2,
C(n-1) + 1
}
base case being
C(n) = 0 for n <= 1
How can I implement this recursion in a pythonic way?
This is an attempt to solve the given problem which I was able to solve successfully but I feel the need to implement a recursive solution also.
Problem 1: Primitive Calculator
You are given a primitive calculator that can perform the following three operations with the current number x: multiply x by 2, multiply x by 3, or add 1 to x. Your goal is given a positive integer n, find the minimum number of operations needed to obtain the number n starting from the number 1.
Problem Description
Task. Given an integer n, compute the minimum number of operations needed to obtain the number n starting from the number 1.
Output Format. In the first line, output the minimum number k of operations needed to get n from 1. In the second line output a sequence of intermediate numbers. That is, the second line should contain positive integers a0, a2,…, a(k-1) such that a0 =1, a(k-1) =n and for all 0≤i<k-1, ai+1 is equal to either ai + 1, 2 x ai, or 3 x ai. If there are many such sequences, output any one of them.
Sample 1.
Input: 5
Output:
3
1 2 4 5
Explanation:
Here, we first multiply 1 by 2 two times and then add 1 ( ((1 x 2) x 2) + 1). Another possibility is to first multiply by 3 and then add 1 two times. Hence “1 3 4 5” is also a valid output in this case.
Sample 2:
Input: 96234
Output:
14
1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234
Explanation:
Again, another valid output in this case is “1 3 9 10 11 33 99 297 891 2673 8019 16038 16039 48117 96234”.
Your goal is to design and implement a dynamic programming solution for this problem. A natural subproblem in this case is the following: C(n) is the minimum number of operations required to obtain n from 1 (using the three primitive operations). How to express C(n) through C(n/3), C(n/2), C(n-1)?
def C(n):
if n <= 1:
return 0
m= C(n-1)
if n % 3 == 0:
m= min(m, C(n/3))
if n % 2 == 0:
m= min(m, C(n/2))
return m + 1
It might be worth to consider memoization.
Cache= {}
def C(n):
global Cache
if n <= 1:
return 0
try:
return Cache[n]
except:
m= C(n-1)
if n % 3 == 0:
m= min(m, C(n/3))
if n % 2 == 0:
m= min(m, C(n/2))
m+= 1
Cache[n]= m
return m
I am unsure whether it is better to test for n <= 1 first.

Keep on halving by integer division until x=1

The function is to take a positive integer n and divide it by 2 (using integer division) until it reaches 1. The output should be the number of times that value is divided. My code so far is
def keep_halving(x):
i=1
for i in range(x):
if x//2==0:
return i
An example of how the function should run is
>>>keep_halving(4)
2
You could try this:
n = int(input("enter number"))
while n > 1:
n //= 2
print (n)
Input: 128
Output:
enter number128
64
32
16
8
4
2
1
This will continuously divide by two using the while loop and stop when n gets to one.
Edit: I read your question too fast. Here is updated code:
count = 0
n = int(input("enter number"))
while n > 1:
n //= 2
print (n)
count +=1
print (count)
This will add one onto count each time n is divided by two.
Input : 128
Output :
enter number128
64
32
16
8
4
2
1
7

Categories

Resources