C
C#2y ago
Name

❔ How to generate all possible multiplications of a given number?

I have this code:
using System;

class GFG
{


static void findCombinationsUtil(int[] arr, int index, int num, int reducedNum)
{

if (reducedNum < 0)
return;


if (reducedNum == 0)
{
for (int i = 0; i < index; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
return;
}


int prev = (index == 0) ?
1 : arr[index - 1];


for (int k = prev; k <= num; k++)
{

arr[index] = k;


findCombinationsUtil(arr, index + 1, num, reducedNum - k);
}
}


static void findCombinations(int n)
{

int[] arr = new int[n];


findCombinationsUtil(arr, 0, n, n);
}


static public void Main()
{

Console.WriteLine("Enter number: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Disintegration is: " + n);
findCombinations(n);
}
}
using System;

class GFG
{


static void findCombinationsUtil(int[] arr, int index, int num, int reducedNum)
{

if (reducedNum < 0)
return;


if (reducedNum == 0)
{
for (int i = 0; i < index; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
return;
}


int prev = (index == 0) ?
1 : arr[index - 1];


for (int k = prev; k <= num; k++)
{

arr[index] = k;


findCombinationsUtil(arr, index + 1, num, reducedNum - k);
}
}


static void findCombinations(int n)
{

int[] arr = new int[n];


findCombinationsUtil(arr, 0, n, n);
}


static public void Main()
{

Console.WriteLine("Enter number: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Disintegration is: " + n);
findCombinations(n);
}
}
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
Name
NameOP2y ago
for example: if I enter 10, I want to get 5x2, 10x1 same as the program I already have but with multiplication
Cattywampus
Cattywampus2y ago
this should do it
int input = 12;
int a = 0; int b = 0;

while(a != input)
{
var mul = a * b;

if(mul == input)
{
Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
int input = 12;
int a = 0; int b = 0;

while(a != input)
{
var mul = a * b;

if(mul == input)
{
Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
(I don't know how to compile it here, don't know the command, really bork ) 2d array should be more appropriate here, but eh.. btw, a simple and fun question... I like it 😻
MODiX
MODiX2y ago
To compile C# code in Discord, use !eval Example:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
MODiX
MODiX2y ago
SlimStv#2835
REPL Result: Success
int input = 12;
int a = 0; int b = 0;

while(a != input)
{
var mul = a * b;

if(mul == input)
{
Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
int input = 12;
int a = 0; int b = 0;

while(a != input)
{
var mul = a * b;

if(mul == input)
{
Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
Console Output
1 x 12
2 x 6
3 x 4
4 x 3
6 x 2
1 x 12
2 x 6
3 x 4
4 x 3
6 x 2
Compile: 631.433ms | Execution: 54.071ms | React with ❌ to remove this embed.
Cattywampus
Cattywampus2y ago
I typed this directly on my browser catfine(also notice a bug there?) I'll fix it later when I get back to my desk
MODiX
MODiX2y ago
SlimStv#2835
REPL Result: Success
int input = 12;
int a = 0; int b = 0;

while(a <= input)
{
var mul = a * b;

if(mul == input)
{
Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
int input = 12;
int a = 0; int b = 0;

while(a <= input)
{
var mul = a * b;

if(mul == input)
{
Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
Console Output
1 x 12
2 x 6
3 x 4
4 x 3
6 x 2
12 x 1
1 x 12
2 x 6
3 x 4
4 x 3
6 x 2
12 x 1
Compile: 592.952ms | Execution: 96.014ms | React with ❌ to remove this embed.
Name
NameOP2y ago
yes. That is what I want but with one small detail: the input must be entered by the user
Cattywampus
Cattywampus2y ago
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 language
Name
NameOP2y ago
I meant, entered in the console
Name
NameOP2y ago
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?
Cattywampus
Cattywampus2y ago
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 math
MODiX
MODiX2y ago
SlimStv#2835
REPL Result: Success
int input = 8;
int a = 0; int b = 0;

while(a <= input)
{
var mul = a * b;

if(mul == input)
{
if(a > b)
break;

Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
int input = 8;
int a = 0; int b = 0;

while(a <= input)
{
var mul = a * b;

if(mul == input)
{
if(a > b)
break;

Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
Console Output
1 x 8
2 x 4
1 x 8
2 x 4
Compile: 624.065ms | Execution: 114.532ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
SlimStv#2835
REPL Result: Success
int input = 20;
int a = 0; int b = 0;

while(a <= input)
{
var mul = a * b;

if(mul == input)
{
if(a > b)
break;

Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
int input = 20;
int a = 0; int b = 0;

while(a <= input)
{
var mul = a * b;

if(mul == input)
{
if(a > b)
break;

Console.WriteLine(a + " x " + b);
}

if(b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
Console Output
1 x 20
2 x 10
4 x 5
1 x 20
2 x 10
4 x 5
Compile: 625.256ms | Execution: 91.037ms | React with ❌ to remove this embed.
Cattywampus
Cattywampus2y ago
@Name
Name
NameOP2y ago
Ah Thank you very much! It looks good
Cattywampus
Cattywampus2y ago
goodluck!
Anton
Anton2y ago
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.
Cattywampus
Cattywampus2y ago
too complicated, no? 😃 its a comparison of same length, so once b > a it will be guaranteed that repetition will occur
Anton
Anton2y ago
Sure, 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
Cattywampus
Cattywampus2y ago
so you only need to calc half of a and forget the rest
Anton
Anton2y ago
you end up doing more operations in your solution, which is fine for small numbers I'm talking theoretically rn
Cattywampus
Cattywampus2y ago
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 when
Anton
Anton2y ago
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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server