Jump to content

How to declare 15 digits variable?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Hi ppl! I have made some code so I paste my source code.
#include <stdio.h> //printf()
#include <conio.h>


int sprawdz(unsigned long long int n, unsigned long long int i){
    unsigned long long int pom=0;
    for(i;i<n;++i){
        pom=n%i;
        if(n%i==0)
        printf("%d\n",n);
    }
}


int main(){

sprawdz(600851475143,1);
return 0;

}
My problem is: I don't know how to send big value into another function. Please help for few helpful hints.

And the codeblock says: integer constant is too large for "long" type.
How should I understand it?

Edited by mokszyk, 17 June 2009 - 11:14 AM.
too many mistakes and etcetera :)

THink positive :)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Creating a custom class to handle large integers, or using something like
http://gmplib.org/
is usually the way to go.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts

WingedPanther said:

Creating a custom class to handle large integers, or using something like
http://gmplib.org/
is usually the way to go.

true but I do not use Linux Distro... so what to do then? I fixed some mistakes.
#include <stdio.h>

void sprawdz(long long int n, long long int i){
    printf("n=%llu i=%llu\n", n,i);
    while(i<n){
        if(n%i==0)
        printf("%d\n",n);
        i++;
    }
}
int main(){

sprawdz(600851475143llu,1llu);
return 0;
}


The question is: why in function sprawdz before loop it prints another numbers than it should be indeed?
THink positive :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I use gmp on both Linux and Windows.

I'd have to compile and run your sample code to see what's happening. Have you compiled with warnings?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts

WingedPanther said:

I use gmp on both Linux and Windows.

I'd have to compile and run your sample code to see what's happening. Have you compiled with warnings?

I am using CodeBlock under Dev C++ / GCC 3.4.2 < (on linux is much more newer :( - and there is everything okay...)

Using Codeblock I get that results:
[in console]:

n=600851475143 i=1

-443946297

-443946297

-443946297

-443946297

-443946297

is it possible to up-to-date my dev? anyway?
THink positive :)

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think the problem is with the conversion specifier %d. Notice that the correct results are using %llu, but the wrong is %d. It seems that %d effectively only processes the first few digits of i, causing garbage data.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Also, you should put
1LLU
not
1llu

Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#8
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts

Aereshaa said:

Also, you should put
1LLU
not
1llu

Hmm, but it shows the same problems.
Done! I fixed and voila :)

#include <stdio.h>
void mnoz(long long int i){
    long long int w=1;
    w=w*i;
    printf("\t%llu\n", w);
}
long long int sprawdz(long long int n, long long int i){
    printf("\n");
    long long int h=1;
    while(i<=n){
        if(n%i==0){
        long long int wynik =1;
        long long int pom=i; //deklarowac
        long long int x=1; //deklarowac
        long long int k=0;//deklarowac
            for(x=1;x<=pom;++x){
                if(pom%x==0)
                    k=k+1;
            }
            if(k=2){
                h=h*i;
                //printf("\ni=%llu\t h=%llu\n",i,h);
                if(h==n){
                    printf("\ni=%llu\t h=%llu\n",i,h);
                    return 0;
                }
            }
        }
        i++;
    }
return 0;
}
int main(){
sprawdz(600851475143llu,1llu);
return 0;
}

Edited by mokszyk, 20 June 2009 - 02:42 AM.
change topic

THink positive :)