[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]

/prog/ - Programming

Programming
Name
Email
Subject
REC
STOP
Comment *
File
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶Show post options & limits]
Confused? See the FAQ.
Options

Allowed file types:jpg, jpeg, gif, png, webp,webm, mp4, mov
Max filesize is16 MB.
Max image dimensions are15000 x15000.
You may upload5 per post.


File: 1428950596261.png (1.08 KB,169x51,169:51,1f12ea246c06418bada30c745a….png)

89a872 No.2032

The equation in pic related describes the behavior of cubes in any number of dimensions, with m being the total number of dimensions in a cube, and f(n m) describes "the number of n dimensional forms in a m dimensional cube."

Here's and example
>For a 2d cube (flat square), m=2. A line segment is n=1. Therefor f(n m)=f(1 2)=4, since a square has four sides

Let's try again and count the edges in a 3d cube
>For a 3d cube, m=3. A line segment is n=1. f(n m)=f(1 3)=12, since there are 12 edges to a cube

Now let's change n=1 (a line) to n=2 (a flat square)
>Fore a 3d cube, m=3. n=2 is a flat square. Therefor f(n m)=f(2 3)=6, since a cube has 6 sides


Now how would I go about writing this in C++? I've been fucking around with it all afternoon and nothing I do seems to work
____________________________
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2045

>>2032
>I've been fucking around with it all afternoon and nothing I do seems to work

What do you have so far? What, specifically, didn't work?
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2051

>>2045
Before I post it how do I use code tags?

(Yes, I'm a newfag. That much should be obvious judging by the fact that I can't do factorials)
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2053

>a 2d cube
W-what.
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2055

The nieve way would be to use recursion, and that would work for some certain size of numbers, but eventually you'll run out of memory and crash. A better solution would be to use memoization so you don't have to build a stack of function calls every time you want to calculate the next factorial.
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2066

code tags are just [co‬de]…[/co‬de]


function factorial(int n) {
int result = 0;
for(int i = 0; i < n; i++) {
result *= i;
}
return result;
}

not sure if it works but it should.
now you just call that with the right values, you should really be able to figure out how.

iirc we used your formula at school for some stochastics calculations but i never heard a geometrical explanation for it or that it has use cases other than calculating some probabilities.
can you give more background info about that formula? too lazy to google it…
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2068

>>2066 oops, i fucked up the function. am not quite awake yet and thought factorials are 1+2+3+4.. then i realized it's got to do with multiplication and changed + to *

anyway, let's try again:
function factorial(int n) {
int result = 1;
for(int i = 1; i < n; i++) {
result *= i;
}
return result;
}


factorial(0) == 1
factorial(1) == 1
factorial(2) == 2
factorial(3) == 6
only works for positive numbers so filter out negatives if your prof requires that.
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2074

>>2051
>Before I post it how do I use code tags?
See: >>>/hdi8/ (the official™how-to board)
For longer code though, use a pastebin.
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2081

>>2053
A line, square (2d "cube"), true cube, and tesseract are all increasing complex iterations of one another
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2096

>>2081
That I know, but I have never seen a square get referred to as 2D cube. It seems a bit stupid considering a cube is implicitly 3D and there is already a word for that.

It is more or less like saying "a third person FPS".
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2188

>>2068

That's fine. It's not for school, it's for fun/autism

I lost my first bit of source code, but I have some notes of it written own in one of my [paper] notebooks.

I'll post a copy of it when I'm done retyping it

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2189

File: 1429718607363.png (59.58 KB,995x685,199:137,Untitled.png)

>>2096

True. Objects with greater than 3 dimensions are still collectively called "(hyper)cubes" for ease of discussion, even if they do have specific names like "tesseract"

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2302

#include <iostream>
using namespace std;

int n;
int m;
int a;
int b;
int c;
int d;
int e;
int f;

//functions//


int factorial1(int n) {
int result = 1;
for(int a = 1; a < n; a++) {
result *= a;
}
return result;
}


int factorial2(int m) {
int result = 1;
for(int b = 1; b < m; b++) {
result *= b;
}
return result;
}


int factorial3(int c) {
int result = 1;
for(int d = 1; d < c; d++) {
result *= d;
}
return result;
}


int subtraction (int n, int m)
{
int c;
c = n - m;
return c;
}

int multiplication (int b, int d)
{
int e;
e = b * d;
return e;
}

int division (int a, int e)
{
int f;
f = a / e;
return f;
}


int main ()
{



cout << "Please enter the number of dimensions of the form in question.\nWe'll call this the n-value.\n";
cin >> n;
cout << "Now we can find the number of m-dimensional forms in an n-dimensional object.\nPlease define the m-value. Note that it should be equal to or less than the value of m.\n";
cin >> m;


a = factorial1 (n);
b = factorial2 (m);
c = subtraction (n, m);
d = factorial3 (c);
e = multiplication (b, d);
f = division (a, e);



cout << f;


system ("PAUSE") ;
return 0;

}

Here's what I have so far. IDK why it doesn't work. For some reason the functions don't seem to be working, so there's no "f" to "cout <<"

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2303

10/10 would laugh again

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2306

>not using permutations to calculate combinations

uint permu(uint x, uint y) {

uint k = x-y;

uint j = x;

while(–j>k) x *= j;

return x;

}

uint comb(uint x, uint y){

return perm(x,y)/fact(y);

}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2312

>>2303

Eh he's just learning

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2320

>>2302

There are a few problems here, namely:

1.) There is no reason to declare all of those variables at the beginning of the file

2.) There is no reason to declare all of those variables in the first place

3.) You should only have one factorial function

4.) The "subtraction", "multiplication", and "division" functions really shouldn't exist

5.) Using the system() function is almost always wrong - in this case, use cin.get() (or better yet, don't lock up the terminal at all)

This:

>https://clbin.com/ROf0S

Will reproduce the table in >>2189

I rearranged the original equation slightly, though.

Bonus points and cake if you can guess why. It wasn't to make it faster.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2322

Using recursion:

int factorial(int a) {

if (a == 1) {

return 1;

}

int b = factorial(a-1);

return a*b;

}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2323

>>2320

Thanks, I'll check it out when I get home. I've just been learning to program over the past few weeks so I'd say 97% of it is stuff I don't know

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2324

Oh, and one other question: why should I use std::cout instead of using namespace std?

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2329

>>2324

>Oh, and one other question: why should I use std::cout instead of using namespace std?

A "namespace" groups things together so that two objects sharing the same name don't conflict with eachother.

For example, if you created your own "cout", like this:

#include <iostream>
#include <fstream>

int main(void)
{
// Please don't actually name your variable "cout"
std::ofstream cout("some_file.txt");

cout << "This gets saved to a file" << std::endl;
std::cout << "This gets printed on the screen" << std::endl;

return 0;
}

This works fine, since "cout" is in a different namespace than "std::cout".

If you were to use "using namespace std" though (and removed all the "std::" parts),

then "This gets printed on the screen" would also be written to the file, since the local

"cout" overloads the global one.

Of course, for simple programs, it doesn't actually matter - the "rule" is for complex programs with lots of namespaces.

tl;dr - Be specific so that the computer and other programmers know what the hell you're trying to do

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2353

>>2322

recursion is bad and has no place in real life applications. write iteratively.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2363

>>2353

>has no place in real life applications

Yeah, totally. Who needs those shitty "parsers" anyway?

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2433

>>2363

Shift-reduce parsers are typically superior to recursive descent parsers. RD is just easier to implement by hand.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.2435

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.4057

>>2032

What the fuck? You calculate n choose k by using pascal's triangle + memoization. Does anyone on /prog/ know how to program?


#include <stdio.h>
#include <stdint.h>
/* col > row is undefined */
uint64_t tb[5000][5000];
uint64_t pascal(const uint64_t row, const uint64_t col) {
if(!col || col == row) return tb[row][col] = 1;
if(tb[row][col]) return tb[row][col];
return tb[row][col] = pascal(row - 1, col - 1) + pascal(row - 1, col);
}
int main() {
uint64_t n, k;
while(scanf("%lld%lld", &n, &k) == 2) {
printf("%lld choose %lld = %lld\n", n, k, pascal(n, k));
}
return 0;
}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

89a872 No.4075

File: 1459192375148.jpg (16.65 KB,251x244,251:244,1242224296128.jpg)

>>4057

>clogging up the cache with a 5000x5000 2d array of 64 bits integers

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.



[Return][Go to top][Catalog][Nerve Center][Random][Post a Reply]
Delete Post [ ]
[]
[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]