so I was given a problem where I have to write a program that the user has to give a positive integer number N and it has to print all numbers up to N but the multiples of 2 must be turned to EPL and multiples of 9 into 032 and for both multiples of 2 and 9 to give EPL 032. I have trouble with the loops as I don't know how to put one into the other.
Related
So I stumbled upon this problem on Super Natural Numbers on HackerEarth, the problem statement goes something like this -
You are given a number n.
A supernatural number is a number whose product of digits is equal to n, and in this number, there is no digit 1.
Count the number of supernatural numbers for a given n.
Input
Contains a single integer n, 1 <= n <= 100.
Output
Print the number of supernatural numbers.
I need someone to brief me on this concept of Super Natural Number and how can I develop a code for it in Python3
My Logic - So, as I come to understand, Super Natural Numbers are numbers that are equal to the product of two digits of another number. For example - Let's take 12, so 12 is equal to the product of 3 and 4 as in 34 or 43, or 2 and 6 as in 26 or 62.
Please correct me if I'm wrong.
The logic for Code - Let me take two lists and have all digits from 1 to 10 as their elements, If I multiply these two lists element-wise, I'll have a list of 100 elements. Then I can search that list for my initial input N and use a counter to find the number of super natural numbers.
I like your logic, but I think you'll miss some.
E.g. 32 = 2x2x2x2x2, so the supernatural numbers of 32 would be 48, 84, 442, 424, 244, 2224, 2242, 2422, 4222 and 22222.
In your logic, you would only find 48 and 84.
While solving some progrmaming problems, I've noticed that the dialogue says:
Input:
Integer N denoting size of array
Next line contains N space separated integers denoting elements in
array
How am I supposed to use the variable N so that it is functioning as supposed to and not just a useless-floating-around input-variable...
I mean, it could just be one input denoting the elements of the array, no need for the length of its elements...
PS: I know that I can just add it there just to pass the problem, I am just asking about if that N variable could be useful using Python (Without the way of a for loop, to ask N number of times for input cause it won't pass the problem).
In Python it usually is, since usually you fetch the entire line at once and process it.
Some programming languages like C++ and Java however tend to benefit from this approach since some parser tools (like Java's Scanner) work by parsing one integer at a time.
You can simply parse your input like:
input() # ignore the 'N'
arr = [int(x) for x in input().split()]
x=list(map(int,input().split())
print(x)
Try this
you will get an array of integers
If I understood you correctly, you want space-separated numbers with the length defined as the input. You can achieve that by:
N = input("Integer N denoting size of array: ")
print(" ".join(str(i + 1) for i in range(int(N))))
e.g.:
Integer N denoting size of array: 12
1 2 3 4 5 6 7 8 9 10 11 12
I am pretty new to python so i dont know many things.
So lets say i got this piece of code
for i in range(10000):
n = random.randrange(1, 1000**20)
ar.append(n)
print(n)
The smallest number i get out of the 10000 is always 3-4 digits smaller than 1000^20
in this particular example the smallest numbers are
2428677832187681074801010996569419611537028033553060588
8134740394131686305349870676240657821649938786653774568
44697837467174999512466010709212980674765975179401904173
50027463313770628365294401742988068945162971582587075902
61592865033437278850861444506927272718324998780980391309
71066003554034577228599139472327583180914625767704664425
90125804190638177118373078542499530049455096499902511032
100371114393629537113796511778272151802547292271926376588
i have tried multiple ranges on multiple occasions and every time i get the same results.Maybe i am doing something wrong?
Okay, so let's use small numbers to illustrate that clearly.
Suppose you pick a random number from 1 to 100, inclusively.
How many 1 digit numbers are there? 9, from 1 to 9. For 2 digits it's from 10 to 99, 90 numbers. For 3 digits it's just 1, 100. Since it's a random pick, the probability of picking one of those 100 (9 + 90 + 9) numbers is equal, and it should be intuitively clear it's 1/100. But since there are 90 2-digit numbers, the probability of picking a 2-digit number is 90/100, or 9/10. For 1-digit numbers it's 9/100, and for the only 3-digit one it's 1/100.
I hope that answers the question.
i want to make a program to find how many number is divisible by 3 or 5 in a number for example 10 has 3 9 6 divisible by 3 and has 5 and 10 divisible by 5 so total is 5 and so on so i write my code
import math
n=float(raw_input())
div3=(n-2)/3
div5=(n-4)/5
f1=math.ceil(div3)
f2=math.ceil(div5)
sumss=f1+f2
print int(sumss)
but in some number it get wrong answer and the range of input number will be
from 1 to 10^18 so i need to use math in it because the time limit for the problem test is 2 second any one have any efficiently equation to make that the loop cant make it it take very long time
This is probably a project Euler question. The issue is that some numbers can be shared by 3 and 5. For instance 22:
divisors of 3: 3 6, 9, 12, 15, 18, 21.
divisors of 5: 5 10, 15, 20
For both 15 occurs, so you did a double count.
The advantage is that 3 and 5 are relatively prime, so the only numbers that are shared are the ones dividable by 15. So you simply need to undo the double counting:
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=div3+div5-div15
print sumss
In case you allow double counting (15 should be counted twice), you can simply use:
n=int(raw_input())
div3=n//3
div5=n//5
sumss=div3+div5
print sumss
Note that the programs omitted the floating point arithmetic: this will result in both faster and more precise programs since floating point numbers work with a limited mantisse and thus can fail to represent a large number correctly (resulting by small errors). Furthermore in general integer arithmetic is faster.
Project Euler #1
Now the problem statement of Project Euler is a bit different: it asks to sum up these numbers. In order to do that, you have to construct an expression to sum up the first k multiples of l:
k
---
\
/ l*i
---
i=1
Using Wolfram Alpha, one gets this expression. So you can calculate these as:
def suml (k,l) :
return k*(k+1)*l/2
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=suml(div3,3)+suml(div5,5)-suml(div15,15)
print sumss
This program gives 119 for n=22 which - you can verify above - is correct if you count 15 only once.
I am not sure whether I got the question right, but here is some idea:
n=float(raw_input())
div3=int(n/3)
div5=int(n/5)
div15=int(n/15)
sumss=div3+div5-div15
print sumss
EDIT: Ah, found the project Euler.
If we list all the natural numbers below 10 that are multiples of 3 or
5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
That is a different task then the question posted here. It says bellow the number and to find the sum.
I am not sure whether it would be the right thing to post the solution here, so I am rather not doing that.
EDIT2: from Project Euler:
We hope that you enjoyed solving this problem. Please do not deprive
others of going through the same process by publishing your solution
outside Project Euler. If you want to share your insights then please
go to thread 1 in the discussion forum.
I'm trying to figure out how many possible ways there are to combine various elements form this string.
"{Hello|Hi|Hey} {world|earth}{!|.|?}"
Where one item (separated by a pipe/|) is selected at random from each group ({}) and combined into a single string.
So the above "template" could produce:
Hello world.
Hi earth?
Hey world.
Hi world?
I'm guessing this is a type of permutation, but I want to make sure I'm getting this right.
It would be really nice if this worked with "n" nested items as well.
"{{Hello|Hi|Hey} {world|earth}|{Goodbye|farewell} {noobs|n3wbz|n00blets}}"
I'd prefer a math/statistics based solution over brute-force looping to get the answer if possible.
Thanks!
Well, there are 3 x 2 x 3 = 18 combinations in your first example.
Your second example is 3 x 4 x 2 x 3 = 72 combinations.
I'm not entirely sure what you mean by {a|b}|{c|d} though, I'm assuming you mean pick one of either (a or b) or (c or d), which is 4 choices.
You might want to read up on combinations here or here.
Update: Yep, it's that simple. Your problem is exactly like counting the number of combinations of digits in a number. For example, if I want to find the number of combinations of an ATM PIN number (4 decimal digits), I have sets {0-9}, {0-9}, {0-9}, {0-9}. There are 10 possibilities for the first choice (= 10). For each of those numbers, there are 10 possibilities for the second choice (= 10 × 10). For each of those, there are 10 for the third (= 10 × 10 × 10) and 10 for the fourth (= 10 × 10 × 10 × 10 = 10,000). It should be intuitively clear that there are 10,000 possibilities for a 4 digit decimal number.
Your example uses sets of words instead of sets of digits, but the principle is the same. The number of combinations is the number of items in set 1 × number of items in set 2 × ... × number of items in set n, etc.
It gets more complicated when you start putting restrictions in, or are picking multiple items from the same set, etc.
The problem breaks down to two simple sub-problems:
count how many combinations are within braces and separated within vbars, for each braces pair
multiply those numbers
So for 1 I'd go with a plain regular expression + looping approach:
import re
def docount(thestring):
x = re.compile(r'{([^}]}')
counts = [mo.group(0).count('|')+1 for mo in x.finditer(thestring)]
result = 1
for c in counts: result *= c
return result
I've embedded 2 as well since that's the most trivial part anyway (if you're keen on using reduce for such purposes, that's OK too in lieu of the last three lines, I guess;-).