Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:35:57+00:00 2026-05-30T02:35:57+00:00

Im trying to learn alot more about C code by trying to do the

  • 0

Im trying to learn alot more about C code by trying to do the examples on the Project Eular Page : http://projecteuler.net/problems
At the moment im trying to work out the highest prime factor from the number 600851475143.
Here is my code:

/* The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?*/
#include <stdio.h>

unsigned long long GetPrimeFactor(unsigned long long number)
{
    /* printf("The number you typed was %d\n", number);*/
    unsigned long long prime = 2;
    unsigned long long LargestPrime = 0;

    while (prime != 0)
    {
        if(number%prime == 0)
        {
            if(number == prime)
            {
                if(LargestPrime < prime)
                    LargestPrime = prime;

                printf("End of prime factors \nLargest Prime: %d\nEnding Number: %d\n", LargestPrime, number);
                break;
            }
            else if(LargestPrime < prime)
                LargestPrime = prime;

            //divide number by prime

            number = number/prime;
            printf("Prime: %d\n", prime);
        }

        else
        {
            //get new prime
            prime = nextPrimeNumber(prime);
        }
    }
}

unsigned long long nextPrimeNumber(unsigned long long input)
{
    unsigned long long nextPrimeNumber;

    while(input !=0)
    {
        if( input < 0)
        {
            printf("The number you have entered is negative\n");
        }
        else if (input > 0)
        {
            nextPrimeNumber = input + 1;

            if(nextPrimeNumber%2 == 0 && nextPrimeNumber != 2)
            {
                nextPrimeNumber += 1;
            }

            while(!isPrime(nextPrimeNumber))
            {
                nextPrimeNumber += 2;
            }

            return nextPrimeNumber;
        }
    }
}

int isPrime(int number)
{
    int i;
    int prime = 1; //true

    if(number == 2)
        prime = 0; //false

    if(number%2 == 0 || number <= 1)
        prime = 0;
    else
    {
        for(i=3; i<sqrt(number) && prime == 1; i+=2)
            if(number%i == 0)
                prime = 0;
    }

    return prime;
}

int main(void)
{
    unsigned long long number;

    printf("Enter a Number: \n");
    scanf("%d", &number);
    GetPrimeFactor(number);

    return 0;
}

This code works for the number 13195 which is in the example, but goes bad when i try the larger numbers and im not sure whats wrong.

output with large number 600851475143: prime: 3 prime: 29 prime: 5108231 etc.
this cant be correct because the number cannot be divisible by 3 anyway.

Cant work out whats going wrong so any help would be great

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:35:58+00:00Added an answer on May 30, 2026 at 2:35 am

    Signed ints go up to 2147483647, unsigned ints go up to 4294967295, you could use unsigned long long which goes up to 18446744073709551615 and if you need something even bigger use an arbitrary-precision/bignum library like GnuMP ( http://gmplib.org/)

    Update to your edited question:

    The format specifier for scanf you use does not even read the whole unsigned long long, i read somewhere that you have to use %llu, however on my gcc this does not work, i have to use:

    printf("Enter a Number: \n");
    scanf("%I64d", &number);
    printf("%I64d", number);  // <- make sure that this prints the number you 
                     // gave it before even calling GetPrimeFactor()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn more about BindingList because I believe that it will
I am trying to learn new stuff about jquery, html, asp .net mvc. I
I've been trying to learn more about private inheritance and decided to create a
I am trying to learn more about Javascript, I have been coding with PHP
Trying to learn a bit about PDO and is going through this tutorial .
Trying to learn about php's arrays today. I have a set of arrays like
I've been trying to learn about Neural Networks for a while now, and I
I've been spending quite alot of time recently trying to learn XAML/C#/WPF/Silverlight, trying to
I am considering starting a project so that I can learn more and keep
I am trying to learn ASP.NET MVC by porting my current app written in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.