[QUOTE=Meatpuppet;39368613]I keep getting an invalid allocation size of 4 gigs. what
[editline]26th January 2013[/editline]
[cpp]void findPrimeNumbers(long long number) {
long *factors = new long[number/2]();
int n=0;
for(int i=0; i<number/2; i++) {
if(number % i == 0) {
factors[n] = i;
cout << factors[n] << "\n";
n++;
}
}[/cpp]
I'm just trying to get the factors first.[/QUOTE]
What are you passing in as "number"?
600,851,475,143
[QUOTE=Meatpuppet;39368613]I keep getting an invalid allocation size of 4 gigs. what
[editline]26th January 2013[/editline]
[cpp]void findPrimeNumbers(long long number) {
long *factors = new long[number/2]();
int n=0;
for(int i=0; i<number/2; i++) {
if(number % i == 0) {
factors[n] = i;
cout << factors[n] << "\n";
n++;
}
}[/cpp]
I'm just trying to get the factors first.[/QUOTE]
You don't need any storage of the factors - you're making a huge array again.
Here's my code:
[cpp]
int main(int argc, char **argv)
{
long long GN = 600851475143; // The number
long long sGN = 775146; // its integer square root
long long try = 3;
for (try; try < sGN; try += 2) // loop up to the square root by 2s
{
if (GN % try == 0)
printf("pF: %d\n", try); // if the big number divides it, print it out
}
}[/cpp]
i don't understand
[QUOTE=Meatpuppet;39368700]i don't understand[/QUOTE]
[cpp]long *factors = new long[number/2]();[/cpp]
This is the part that's causing the problem - you don't need 300 billion longs to count the factors. If you want, you can print them out as you find them, rather than storing them.
So, decided to make the leap to modern OpenGL (from deprecated), using Java + LWJGL and Overv's [URL="http://open.gl"]Open.gl[/URL].
In basic, I have a bug when moving from Overv's intro to 3D to his cube implementation (found [URL="http://open.gl/transformations"]here[/URL] and [URL="http://open.gl/depthstencils"]here[/URL] respectively). It's one I've done a bunch of research into and tried many times to fix, but have learned almost nothing about and still have not managed to fix. I posted a StackOverflow question about it, and thought (given that we seem to have a lot of OpenGL buffs here) that it might be useful to post it here, too...no douchebaggery intended, I'm just very tired of such a simple implementation not working.
[URL="http://stackoverflow.com/questions/14532516/opengl-vbo-cube-not-rendering-correctly-java-lwjgl"]Here's the question[/URL] (containing source, detail, where I think the error is, test cases, and a live demo of a programmer going insane). I'm obviously willing to give any and all extra information I can so as to finally reach a conclusion on where I'm being stupid in my code.
i don't understand the loop though
[QUOTE=Meatpuppet;39368807]i don't understand the loop though[/QUOTE]
Oh, okay. So we start try at 3 because we know the number is odd, and so not divisible by 2. We go up by 2 for the same reason - every other number is even, and the big number is odd. And we go up to the square root because once you have all the factors below the square root, you can find the higher ones by dividing by the lower ones.
(For example, 36:)
[code]
1 2 3 4 [6] 9 12 18 36
If we have 1, 2, 3, 4, and 6, we can find: 36 / 2 = 18, 36 / 3 = 12, 36 / 4 = 9, 36 / 6 = 6.
[/code]
is it normal to make try a variable like that, that just seems really really silly
I'm not the best explainer, so I hope you understand me okay. And luckily for this problem the answer is one of the factors below the square root, so you don't have to find the higher ones.
[editline]26th January 2013[/editline]
[QUOTE=Shadaez;39368884]is it normal to make try a variable like that, that just seems really really silly[/QUOTE]
How would you do it without one...?
[QUOTE=account;39368886]I'm not the best explainer, so I hope you understand me okay. And luckily for this problem the answer is one of the factors below the square root, so you don't have to find the higher ones.
[editline]26th January 2013[/editline]
How would you do it without one...?[/QUOTE]
I mean naming it try is silly to me
also there aren't any prime factors of the number above the square root, which is why you don't check the ones above it
[QUOTE=Shadaez;39368928]I mean naming it try is silly to me
also there aren't any prime factors of the number above the square root, which is why you don't check the ones above it[/QUOTE]Oh, I think try makes sense since it is the number we're [i]try[/i]ing to divide by :v:
i knew about the square root part, i just didn't know why he was using 3. thanks!
man that code really confused me, I thought you were using [url=http://www.cplusplus.com/doc/tutorial/exceptions/]try[/url].
you shouldn't name your variables that
[QUOTE=Shadaez;39368964]man that code really confused me, I thought you were using [url=http://www.cplusplus.com/doc/tutorial/exceptions/]try[/url].
you shouldn't name your variables that[/QUOTE]
Haha, I was programming in C which doesn't have try/catches. Yeah if I move to C++ or something I really have to remember that
Also, what are the main differences between printf and cout? And how do I use printf? I've seen the %d or %f, I still don't quite understand that. Does the %d or %f just fill in based on whats after the comma?
[QUOTE=Meatpuppet;39368983]Also, what are the main differences between printf and cout? And how do I use printf? I've seen the %d or %f, I still don't quite understand that. Does the %d or %f just fill in based on whats after the comma?[/QUOTE]
I don't know cout well, but it seems pretty nice because it always prints whatever you give it in the appropriate manner.
And yeah, printf is for formatted output, and you can do a lot of things with the formatting.
[url]http://www.cplusplus.com/reference/cstdio/printf/[/url] for reference, the highlights are:
[code]%s for strings
%d/%i for integers
%.nf (where n is an integer) for floats precise to n digits
%x for hex numbers
%p for pointers
Numbers can be used to pad:
%03x - Hex numbers with leading 0s to reach 3 characters
[/code]
Oh, but beware in C++ because printf takes C strings, not std::strings.
Ok, so here's a weird thing. The first four numbers it prints out are always correct. The last 3 are always not prime. What?
It is the same for 13195 and this number. I tried the fourth number on the big number and got the answer right. Hmmm...
[QUOTE=Meatpuppet;39369151]Ok, so here's a weird thing. The first four numbers it prints out are always correct. The last 3 are always not prime. What?
It is the same for 13195 and this number. I tried the fourth number on the big number and got the answer right. Hmmm...[/QUOTE]
Remember, this loop just finds the factors of the number - it doesn't say anything about whether they're prime or not. It is odd that it follows that pattern though
All of the primality tests I find are too complicated for my tiny brain to put into code :(
If you have some time, read the first chapter of this:
-snipped-
It's about implementing RSA encryption, but a large chunk is about using Fermat's little theorem to make an efficient primality-testing algorithm.
Mind that it's all very number theoretical and hard to grasp, so just try your best. I never really actually made the algorithm, just [i]sort of[/i] understood it.
And I agree with Larrikang below me, the factors of that Project Euler number are mostly small enough to test with Erastothenes' sieve that you made before.
[QUOTE=Meatpuppet;39369151]Ok, so here's a weird thing. The first four numbers it prints out are always correct. The last 3 are always not prime. What?
It is the same for 13195 and this number. I tried the fourth number on the big number and got the answer right. Hmmm...[/QUOTE]
So you've found all* of the factors, now you need to know which of those factors are actually prime. You are already equipped to solve this part of the problem just by reusing code you've written.
* You're algorithm only finds half of the factors, you need to check the other half as well by dividing the number by each of the factors you've found.
[QUOTE=Larikang;39369476] You are already equipped to solve this part of the problem just by reusing code you've written.
[/QUOTE]
man i feel stupid
[QUOTE=Meatpuppet;39369386]All of the primality tests I find are too complicated for my tiny brain to put into code :([/QUOTE]
[cpp]#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
typedef long int num;
typedef struct num_array
{
num* nums;
int size;
} num_array;
#define array_bytes(ARRAY) (sizeof(num) * ARRAY->size)
num_array* make_array(int size)
{
num_array* array = malloc(sizeof(num_array));
array->size = size;
array->nums = malloc(array_bytes(array));
return array;
}
void free_array(num_array* array)
{
free(array->nums);
free(array);
}
num_array* get_primes_to(num limit)
{
/* This function uses the Sieve of Eratosthenes to find primes. Look it up. */
/* We need a truth table of whether or not any numbered index is a prime. */
num_array* prime_table;
num IS_PRIME = 0;
num NOT_PRIME = -1;
/* We also need an array of the primes we've found. */
num_array* primes;
int primesFound = 0;
int index; /* An iterator, for an index in the truth table. */
int i; /* An iterator, for general use. */
/* Create the table and assume all numbers are primes at first. */
prime_table = make_array(limit + 1);
memset(prime_table->nums, IS_PRIME, array_bytes(prime_table));
/* Except these. */
prime_table->nums[0] = NOT_PRIME;
prime_table->nums[1] = NOT_PRIME;
for(index = 0; index < prime_table->size; ++index)
{
if(prime_table->nums[index] == IS_PRIME)
{
/* Huzzah, a prime! Now mark all its multiplications as not primes. */
++primesFound;
for(i = (index * 2); i < prime_table->size; i += index)
{
prime_table->nums[i] = NOT_PRIME;
}
}
}
/* Make our array of primes and fill it with primes we've found. */
primes = make_array(primesFound);
/* Loop through the table, and append primes to our new array. */
for(index = 0, i = 0; index < prime_table->size; ++index)
{
if(prime_table->nums[index] == IS_PRIME)
{
primes->nums[i++] = index;
}
}
free_array(prime_table);
return primes;
}
int main(void)
{
num integer = 600851475143;
num_array* primes; /* A list of primes that could be factors of integer. */
int i;
primes = get_primes_to(sqrt(integer)); /* Cap at sqrt to save resources. */
/* Go through the list of primes backwards to find the largest factor. */
for(i = (primes->size - 1); i > 0; --i)
{
if(fmod(integer, primes->nums[i]) == 0)
{
/* Found it, print it. */
printf("%ld\n", primes->nums[i]);
break;
}
}
free_array(primes);
return 0;
}[/cpp]
I used [url=https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes]Sieve of Eratosthenes[/url] to make an array of all the prime numbers up to sqrt(number), then looped through the array backwards to find the largest prime factor the number could be divided by.
well i got my code to list all the factors
@Jookia:
That's a nice solution. I assume it runs fast, but it uses a good amount of space. sizeof(long) * 775147 bytes
[QUOTE=account;39369572]@Jookia:
That's a nice solution, I assume it runs fast, but it uses a ton of space. sizeof(long) * 775147 bytes[/QUOTE]
Unoptimized, running it under Valgrind which should slow it down by 50 times or whatever, I get this:
[code][jookia@jookia-arch build]% time valgrind ./3
==10087== Memcheck, a memory error detector
==10087== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==10087== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==10087== Command: ./3
==10087==
6857
==10087==
==10087== HEAP SUMMARY:
==10087== in use at exit: 0 bytes in 0 blocks
==10087== total heap usage: 4 allocs, 4 frees, 6,698,112 bytes allocated
==10087==
==10087== All heap blocks were freed -- no leaks are possible
==10087==
==10087== For counts of detected and suppressed errors, rerun with: -v
==10087== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
valgrind ./3 0.52s user 0.04s system 99% cpu 0.561 total[/code]
6MB memory use, takes half a second.
I've got to write an object loader for an assignment, it's supposed to read a .obj file and use it to display a model on the screen. The example I've got at the moment is supposed to display a teapot. I've been able to get it to display a model that looks like a teapot, but the vertices aren't joined up correctly. This is the picture of the teapot (I fixed the graphical errors on the top):
[url=http://i.imgur.com/XUReINB.png][img]http://i.imgur.com/XUReINBs.png[/img][/url]
A close up shot:
[url=http://i.imgur.com/aYbJOWD.png][img]http://i.imgur.com/aYbJOWDs.png[/img][/url]
This is the code that reads the .obj file:
[cpp]
ObjModelLoader::ObjModelLoader(string fileName, vector<xyVertices>& vertices, vector<GLushort>& indices, vector<xyVertices>& vNormals)
{
xyVertices coords;
//vertices.push_back(coords);
//vertices[0].x = 0.f;
//vertices[0].y = 0.f;
//vertices[0].z = 0.f;
//indices.push_back(0);
ifstream file;
file.open(fileName);
stringstream ss;
if (file.is_open())
{
while (!file.eof())
{
float verticesBuffer[3];
int indicesBuffer[3];
while (std::getline(file, buffer))
{
if (buffer == "") continue;
ss.str(buffer);
ss.ignore(2);
switch(buffer[0])
{
case 'v': {
ss >> verticesBuffer[0] >> verticesBuffer[1] >> verticesBuffer[2];
vertices.push_back(coords);
int j = vertices.size()-1;
vertices[j].x = verticesBuffer[0];
vertices[j].y = verticesBuffer[1];
vertices[j].z = verticesBuffer[2];
ss.clear();
break;
}
case 'f': {
ss >> indicesBuffer[0] >> indicesBuffer[1] >> indicesBuffer[2];
for (int j=0; j<3; ++j) {
indices.push_back(indicesBuffer[j]);
}
ss.clear();
break;
}
}
}
}
//crossProduct(vertices, indices, vNormals);
}
}
[/cpp]
This is the draw() function:
[cpp]
void draw()
{
glBegin(GL_QUADS);
glColor3f(1.f, 0.f, 0.f);
for (int i=0; i<indices.size(); i++)
{
glVertex3f(vertices[indices[i]-1].x, vertices[indices[i]-1].y, vertices[indices[i]-1].z);
}
glEnd();
}
[/cpp]
Can anyone see anything wrong with this? I've been going over this all day, haven't been able to figure it out yet.
[QUOTE=st_nick5;39369780]Can anyone see anything wrong with this? I've been going over this all day, haven't been able to figure it out yet.[/QUOTE] Obj files use indices starting at 1, whereas OpenGL expects indices starting at 0. Doesn't look like you're accounting for that.
[QUOTE=bean_xp;39369947]Obj files use indices starting at 1, whereas OpenGL expects indices starting at 0. Doesn't look like you're accounting for that.[/QUOTE]
Thought I was, in the draw() function, I have vertices[indices[i]-1].x, etc. Wouldn't that do it?
Sorry, you need to Log In to post a reply to this thread.