Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
C/C++ Coding challenges
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="madurax86" data-source="post: 8246507" data-attributes="member: 1293"><p>YAY! I finally did it! This doesn't repeat the partitions, it uses a bit of mathematics theory for this. But hey, it gets the job done!.</p><p>Read this,</p><p><a href="http://mathworld.wolfram.com/Partition.html" target="_blank">http://mathworld.wolfram.com/Partition.html</a></p><p></p><p>My algorithm uses the equation given in that page(1.x1+2.x2+3.x3 .....n.xn=n) to find the partitions, the program finds roots of that equation, that's all!</p><p></p><p>[code]</p><p>#include <stdio.h></p><p>#include <stdlib.h></p><p></p><p>typedef unsigned int uint;</p><p>typedef struct anode{</p><p> uint num;</p><p> uint level;</p><p> struct anode *parent;</p><p>}node;</p><p></p><p>uint n=0;</p><p></p><p>node* alloc()</p><p>{</p><p> node *new = malloc(sizeof(node));</p><p> new->num = 0;</p><p> new->level = 0;</p><p> new->parent = NULL;</p><p> return new;</p><p>}</p><p>void backtrace(node *a)</p><p>{</p><p> uint i;</p><p> static uint count=0;</p><p> count++;</p><p> printf("%d. ",count);</p><p> while (a->parent!=NULL){</p><p> for (i=0;i<a->num;i++)</p><p> printf("%d ", a->level);</p><p> a = a->parent;</p><p> }</p><p> printf("\n");</p><p>}</p><p></p><p>void sum(uint total, uint level, node *owner)</p><p>{</p><p> uint i;</p><p> uint tmp = 0;</p><p> node *this;</p><p></p><p> if ( level > n ) return;</p><p> for (i=0;i<=n;i++){</p><p> </p><p> tmp = total + level*i;</p><p> this = alloc();</p><p> this->parent = owner;</p><p> this->num = i;</p><p> this->level = level;</p><p></p><p> if (tmp >= n){</p><p> if (tmp == n) backtrace(this);</p><p> return;</p><p> }else{</p><p> sum(tmp, level+1, this);</p><p> }</p><p> }</p><p></p><p>}</p><p></p><p>int main()</p><p>{</p><p> node *root = alloc();</p><p> printf("Enter a number : ");</p><p> scanf("%d",&n);</p><p> </p><p> sum(0, 1, root);</p><p> return 0;</p><p>}</p><p>[/code]</p></blockquote><p></p>
[QUOTE="madurax86, post: 8246507, member: 1293"] YAY! I finally did it! This doesn't repeat the partitions, it uses a bit of mathematics theory for this. But hey, it gets the job done!. Read this, [url]http://mathworld.wolfram.com/Partition.html[/url] My algorithm uses the equation given in that page(1.x1+2.x2+3.x3 .....n.xn=n) to find the partitions, the program finds roots of that equation, that's all! [code] #include <stdio.h> #include <stdlib.h> typedef unsigned int uint; typedef struct anode{ uint num; uint level; struct anode *parent; }node; uint n=0; node* alloc() { node *new = malloc(sizeof(node)); new->num = 0; new->level = 0; new->parent = NULL; return new; } void backtrace(node *a) { uint i; static uint count=0; count++; printf("%d. ",count); while (a->parent!=NULL){ for (i=0;i<a->num;i++) printf("%d ", a->level); a = a->parent; } printf("\n"); } void sum(uint total, uint level, node *owner) { uint i; uint tmp = 0; node *this; if ( level > n ) return; for (i=0;i<=n;i++){ tmp = total + level*i; this = alloc(); this->parent = owner; this->num = i; this->level = level; if (tmp >= n){ if (tmp == n) backtrace(this); return; }else{ sum(tmp, level+1, this); } } } int main() { node *root = alloc(); printf("Enter a number : "); scanf("%d",&n); sum(0, 1, root); return 0; } [/code] [/QUOTE]
Insert quotes…
Verification
Awruddata maasa keeyada?
Post reply
Top
Bottom