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.
Related
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.
I'm stuck in Codewars Kata, I hope someone could help me out (without spoiling the solution).
In fact the problem is that I didn't fully understand how it should work, I got the idea of the exercise but things are a bit confusing especially in Sample tests.
Here are the instructions:
The number 81 has a special property, a certain power of the sum of its digits is equal to 81 (nine squared). Eighty one (81), is the first number in having this property (not considering numbers of one digit). The next one, is 512. Let's see both cases with the details.
8 + 1 = 9 and 9^2 = 81
512 = 5 + 1 + 2 = 8 and 8^3 = 512
We need to make a function, power_sumDigTerm(), that receives a number n and may output the nth term of this sequence of numbers. The cases we presented above means that:
power_sumDigTerm(1) == 81
power_sumDigTerm(2) == 512
And here are the sample tests:
test.describe("Example Tests")
test.it("n = " + str(1))
test.assert_equals(power_sumDigTerm(1), 81)
test.it("n = " + str(2))
test.assert_equals(power_sumDigTerm(2), 512)
test.it("n = " + str(3))
test.assert_equals(power_sumDigTerm(3), 2401)
test.it("n = " + str(4))
test.assert_equals(power_sumDigTerm(4), 4913)
test.it("n = " + str(5))
test.assert_equals(power_sumDigTerm(5), 5832)
My main problem is how did they get the results for the sample tests.
A good speed up trick is to not check all numbers, Any such number must be of the form a^b for integers a and b. If you find a way to enumerate those and check them you will have a fairly efficient solution.
On f(5), the sum of the numbers is 5+8+3+2 = 18. And 18^3=5832.
Brute force method would look like this for the next one:
Start at 5833, add the digits, check the powers of the sum to see if you get number. This would actually be very fast as you can see that last one only got to ^3. As soon as the power is larger than the number you are seeking, move on to the next number: 5834. When you find one, insert into a table to remember it.
The number theory experts may be able to find a more efficient method but this brute force method is likely to be pretty fast.
Grab a prime generator; you need only prime powers to generate the sequence (although you will have all integers >= 2 in the test for inclusion). This is because if a number is a composite power, it's also a prime power.
Maintain a list of current powers, indexed by the base integer. For instance, once you've made it to a limit of 100, you'll have the list
[0, 0, 64, 81, 64, 25, 36, 49, 64, 81, 100]
// Highest power no greater than the current limit
... and the current list of target numbers has only one element: [81]
Extending the limit:
Pick the lowest number in the list (25 = 5^2, in this case)
Multiply by its base: 25 => 125
Check: is 1+2+5 a root of 125? (There are minor ways to speed this up)
If so, add 125 to the list
Now, go back to all lower integers (the range [2, 5-1] ), and add any smaller prime powers of those integers. (I haven't worked out whether there can ever be more than one power to add for a given integer; that's an interesting prime-based problem.)
Whenever you add a new target number, make sure you insert in numerical order; the step in the previous paragraph may introduce a "hit" lower than the one that triggered the iteration. For instance, this could append 5832 before in finds 4913 (I haven't coded and executed this algorithm). You could collect all the added target numbers, sort that list, and append them as a block.
Although complicated, I believe that this will be notably faster than the brute-force methods given elsewhere.
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 have some kind of AP in Computer Science and I've started on coding/programming with Python. I briefly used C++ for few years and now I switched to Python. I don't really remember where I stopped with C++ but that's irrelevant now. Anyways, I have this task that says: "Write program that loads number N, then N numbers and prints number that has the highest value between them. Number N has input in one line and line underneath loads N numbers with same space between them. None of the numbers will be greater than 100. Number N will be greater than 2."
I wrote this code;
`n = int (input())
max = 2
for i in range (1, n+1, 1):
x=map(int,input().split())
if x>max: x=max
print (max)
`
which returned this error:
5
2 87 12 90 55
File "C:\Users\Mariee.Marija-PC\Documents\Python\19-4.py", line 5, in
if x>max: x=max
TypeError: unorderable types: map() > int()
that was totally expected because I know I can't compare those two as they are clearly not comparable (which again, I am very aware of).
So my question is, is there any other way that N number could be put in one line and then N numbers could be put in the same line but you can compare them (if that makes any sense).
[P.S. Also I'm ultimately sorry if my english was bad.]
The variable x is a map (similar to a list) of integers, but you are comparing it with a single integer.
In order to loop over the values of x you should use a for loop:
for x in map(int, input().split()):
if x > max:
max = x
There's already a max method in Python, and no need to re-create a max function like you have to do in C/C++. Just apply the max method
n = int (input())
print (max(map(int,input().split())))
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.