❔ How to generate all possible multiplications of a given number?
I have this code:
And what it does is it generates all the sums of a given number. Here's how it works:
Input: 3
Output: 1 1 1
2 1
So I enter any number I want and it generates all possible sums of that number.
I want to do the same but with multiplications. Anyone know how?
24 Replies
for example: if I enter 10, I want to get 5x2, 10x1
same as the program I already have but with multiplication
this should do it
(I don't know how to compile it here, don't know the command, really )
2d array should be more appropriate here, but eh..
btw, a simple and fun question... I like it 😻
To compile C# code in Discord, use
!eval
Example:
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!SlimStv#2835
REPL Result: Success
Console Output
Compile: 631.433ms | Execution: 54.071ms | React with ❌ to remove this embed.
I typed this directly on my browser (also notice a bug there?)
I'll fix it later when I get back to my desk
SlimStv#2835
REPL Result: Success
Console Output
Compile: 592.952ms | Execution: 96.014ms | React with ❌ to remove this embed.
yes. That is what I want but with one small detail: the input must be entered by the user
well yeah? just replace the
input
with whatever you want there
Do your best 1st and if you're stuck post it here, being spoonfed would not help you learn the languageI meant, entered in the console
ok, I will try it
nice, it works!
1x12 and 12x1 is the same. What should I do if I don't want any repetition?
hi, sorry was busy with irl stuff..
bcos we're comparing 2 values of the same range, so once the value of
a
bigger than the value of b
you'd need to break out of the loop so the repetition won't happen. It's a simple mathSlimStv#2835
REPL Result: Success
Console Output
Compile: 624.065ms | Execution: 114.532ms | React with ❌ to remove this embed.
SlimStv#2835
REPL Result: Success
Console Output
Compile: 625.256ms | Execution: 91.037ms | React with ❌ to remove this embed.
@Name
Ah Thank you very much! It looks good
goodluck!
The best way is to break down the number into its prime factorization, like
72 = 2 × 2 × 2 × 3 × 3 = 2^3 × 3^2
And then go through all combinations of these:
(2^0 × 3^0) × (2^3 × 3^2) = 1 × 72
(2^1 × 3^0) × (2^2 × 3^2) = 2 × 36
(2^2 × 3^0) × (2^1 × 3^2) = 4 × 18
(2^3 × 3^0) × (2^0 × 3^2) = 8 × 9
(2^0 × 3^1) × (2^3 × 3^1) = 3 × 24
(2^1 × 3^1) × (2^2 × 3^1) = 6 × 12
After that the combinations repeat in the previous products.
too complicated, no? 😃
its a comparison of same length, so once
b
> a
it will be guaranteed that repetition will occurSure, it'll require more logic. But basing your solution on that principle is required for being able to produce the fastest possible code for larger numbers
so you only need to calc half of
a
and forget the restyou end up doing more operations in your solution, which is fine for small numbers
I'm talking theoretically rn
that was a leftover from 1st solution... it can be much less code for sure with his updated question
and the
a > b
can be eliminated entirely
also yeah, I should've updated it rather than copy pasting previous answer it doesn't matter really what you do to your code, it won't become optimal. It will have larger complexity than if you used the principal I stated above
your current solution is O(N)
ah no
O(N^2)
because there are two variables that you increment until you number (or half your number)
sieve of Eratosthenes has better complexity
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.