LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2017, 12:23 PM   #106
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331

Quote:
Originally Posted by pan64 View Post
will lead to 3.4 sec, which is approximately 15% of "original" execution time.
Nice optimizations! Executes in 3.866 sec. on my old AMD phenom.
Here is your version using libgmp:
Code:
// g++ main.cpp -lgmp -lgmpxx

#include <iostream>
#include <vector>
#include <algorithm>
#include <gmpxx.h>

using std::cout;
using std::vector;
typedef mpz_class mpz;

static vector<mpz> powers(12);

bool next_comb_rep(vector<size_t> &vec, int place) {
    if(place == 0){
        vec[place] += 1;
    } else {
        if(next_comb_rep(vec, place-1)) {
            vec[place] += 1;
            for(int j=0; j<place; ++j) {
                vec[j] = vec[place];
            }
        }
    }
    return vec[place]==10;
}

mpz sum_of_power_of_digit(vector<size_t> &digits, int power) {
    mpz sum = 0;
    for(int i=0; i<power; ++i) {
        sum += powers[digits[i]];
    }
    return sum;
}

bool del (vector<size_t> &v, mpz i_mpz) {
    unsigned i = i_mpz.get_ui();
    auto it = std::find(v.begin(), v.end(), i);
    if(it == v.end())
        return false;
    v.erase(it);
    return true;
}

int main() {
    for(int base=23; base<24; ++base) {
        cout << "\n" << base << " digits:\n";
        vector<size_t> vec(base);
        vector<mpz> out_vec;
        for (int i=1; i<12; i++) {
             powers[i] = i;
             for(int pow=1; pow<base; ++pow) {
                 powers[i] *= i;
             }
       }
        while(1){
            if(next_comb_rep(vec, base-1)) {
                break;
            }
            mpz num = sum_of_power_of_digit(vec, base);
            mpz tmp_num = num;
            vector<size_t> vec1 = vec;
            while(tmp_num>0){
                if ( ! del(vec1, tmp_num % 10) )
                    break;
                tmp_num /=10;
            }
            if (vec1.size() != 0 )
                 continue;
            out_vec.emplace_back(num);
        }
        std::sort(out_vec.begin(), out_vec.end());
        for(mpz i : out_vec){
            cout << i << "\n";
        }
    }
}
As I stated it runs slower (1-19 digits in 15.342 sec) but finds the larger Armstrong numbers:
Code:
23 digits:
21887696841122916288858
27879694893054074471405
27907865009977052567814
28361281321319229463398
35452590104031691935943

real    0m27.061s
user    0m27.056s
sys     0m0.000s
 
Old 07-03-2017, 05:35 AM   #107
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,207

Rep: Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419
Code:
 n    f(n)    n    f(n)    n    f(n)     n     f(n)
30   12.71   40    5.91   50	2.57    60    1.07
31   11.82   41    5.45   51	2.36	61    0.98
32   10.98   42    5.02   52	2.17
33   10.19   43    4.63   53	1.99
34    9.45   44    4.26   54	1.82
35    8.76   45    3.92   55	1.67
36    8.11   46    3.61   56	1.53
37    7.50   47    3.32   57	1.40
38    6.93   48    3.05   58	1.28
39    6.40   49    2.80   59	1.17.
f(n) = n*9^n/10^(n-1)
n is the number of digits, f(n) is the sum of powers of the digits of the biggest n-digit number (n times 9).

based on this function we can see there can be no narcissistic number for n>60, and we can also see:
for 52<n<61 the narcissistic number must begin with 1, so vec should contain at least one 1, which will give us a very good restriction to speed up...
for 48<n<53 we must have at least a 1 or 2 in vec
...
for n<34 unfortunately this logic will not help.
But we can be much stricter. If the given number must contain a 1, f(n) will be n*9^(n-1)/10^(n-1), which will mean there can be no narcissistic number for n>52 ...
 
Old 07-03-2017, 09:15 AM   #108
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Quote:
Originally Posted by Laserbeak View Post
I'd like someone do it in a pure shell script lol.
This awk ...
Code:
time awk -F "" 'BEGIN{limit=9999999;
   for (num=1;num<=9;num++) for (pwr=3;pwr<=9;pwr++) p[num,pwr]=num^pwr;
   for (j=101;j<=limit;j++) {ex=split(j,a);   # ex = exponent
     cand=p[a[1],ex]
     for (k=2;k<=ex;k++) cand=cand+p[a[k],ex]  # Candidate Armstrong Number
     if (cand==j) print "Armstrong Number:,j}}' >$OutFile1
echo "OutFile1 ..."; cat $OutFile1; echo "End Of OutFile1 ($(wc -l <$OutFile1) lines)"
... produced this result ...
Code:
real	0m46.384s
user	0m46.376s
sys	0m0.004s
OutFile1 ...
Armstrong Number: 153
Armstrong Number: 370
Armstrong Number: 371
Armstrong Number: 407
Armstrong Number: 1634
Armstrong Number: 8208
Armstrong Number: 9474
Armstrong Number: 54748
Armstrong Number: 92727
Armstrong Number: 93084
Armstrong Number: 548834
Armstrong Number: 1741725
Armstrong Number: 4210818
Armstrong Number: 9800817
Armstrong Number: 9926315
End Of OutFile1 (15 lines)
That execution time is not competitive with posted solutions using other languages.

Daniel B. Martin
 
1 members found this post helpful.
Old 07-03-2017, 03:57 PM   #109
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
This code ...
Code:
function Method3 {
echo; echo "Method #3 of LQ Member danielbmartin, 3,4,5,6, and 7-digit integers."
seq 101 9999999  \
|awk -F "" 'BEGIN{for (num=1;num<=9;num++) for (pwr=3;pwr<=9;pwr++) p[num,pwr]=num^pwr};
   {cand=p[$1,NF]
     for (k=2;k<=NF;k++) cand=cand+p[$k,NF]  # Candidate Armstrong Number
     if (cand==$0) print "Armstrong Number:",$0}' >$OutFile3
}

time Method3
echo "OutFile3 ..."; cat $OutFile3; echo "End Of OutFile3 ($(wc -l <$OutFile3) lines)"
...produced this result ...
Code:
Method #3 of LQ Member danielbmartin, 3,4,5,6, and 7-digit integers.

real	0m31.251s
user	0m31.400s
sys	0m0.036s
OutFile3 ...
Armstrong Number: 153
Armstrong Number: 370
Armstrong Number: 371
Armstrong Number: 407
Armstrong Number: 1634
Armstrong Number: 8208
Armstrong Number: 9474
Armstrong Number: 54748
Armstrong Number: 92727
Armstrong Number: 93084
Armstrong Number: 548834
Armstrong Number: 1741725
Armstrong Number: 4210818
Armstrong Number: 9800817
Armstrong Number: 9926315
End Of OutFile3 (15 lines)
I did not expect two piped commands to perform better than the single awk in post #108 but it did. Perhaps that is because the split in #108 is not needed.

Daniel B. Martin
 
Old 07-04-2017, 06:14 PM   #110
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by Laserbeak View Post
I'd like someone do it in a pure shell script lol.
Great idea. How about bash?
Code:
#!/bin/bash
# search for Armstrong numbers within a specified range

for number in $(seq $1 $2 $3) # script takes 1, 2, or 3 arguments (as per seq)
do
    power=${#number} # get number of characters
    power_sum=0      # initialize sum
    for digit in $(grep -o . <<< $number) # this grep lists single characters
    do
        ((power_sum+=digit**power))  # bash variable arithmetic
    done
    if [ $power_sum == $number ]  # == is string comparison operator
    then                          # -eq (integer comparison) works just as well
        echo $number
    fi
done
Normal test:
Code:
time ./arm_range.sh 100 10000
153
370
371
407
1634
8208
9474

real	0m43.473s
user	0m2.744s
sys	0m3.052s
On my system, it works up to 19 digits (64-bit arithmetic):
Code:
 ./arm_range.sh 4929273885928088820 4929273885928088830
4929273885928088826

Last edited by Beryllos; 07-04-2017 at 08:11 PM. Reason: eliminated unnecessary $((...)) inside arithmetic expression
 
1 members found this post helpful.
Old 07-04-2017, 08:39 PM   #111
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,813

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
Couple of small changes
Code:
#!/usr/bin/env bash

power=$1

for (( i = 0; i <= 9; i++ ))
do
  raised+=( $(( i**power )) )
done

(( max = 10**power ))
(( min = 10**(power - 1) ))

for (( num = min; num < max; num++ ))
do
  sum=0
  for (( i = power - 1; i >= 0; i-- ))
  do  
    (( sum > max )) && break
    (( sum += raised[${num:i:1}] ))
  done

  (( sum == num )) && echo $num
done
To run:
Code:
$ time ./arm.sh 3
153
370
371
407

real	0m0.201s
user	0m0.190s
sys	0m0.000s
$ time ./arm.sh 4
1634
8208
9474

real	0m2.190s
user	0m2.100s
sys	0m0.003s
Obviously it just gets slower as the numbers increase
 
1 members found this post helpful.
Old 07-05-2017, 09:39 AM   #112
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by grail View Post
Couple of small changes ...
Looks good, and fast... Well, it's only bash, so it's fast in the same way that a Ford Model T is faster than a horse and buggy, while the better C/C++ programs are rockets.
 
Old 07-05-2017, 02:23 PM   #113
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
Quote:
Originally Posted by GazL View Post
Thanks to all those who took an interest in this topic. Best Necro post ever!
This has been a pleasure to follow - and very well done everyone!

*(I am so glad that I refrained from posting my initial clumsy method among such astute people! )
 
Old 07-05-2017, 04:51 PM   #114
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Multiprocessor Objective-C

Multithreading!

OK, I've done a multi-threaded version in Objective-C for Mac OS X. I'm still working on it, so I don't have any speed numbers. I have to finish it and then figure out how to set Xcode to compile it with optimization and in release not debug mode. But if there are any Mac programmers, please have a look and give me any ideas. It's kind a strange Mac program since it's in Objective-C but just a terminal program (at least for now).

Here are the files:

Code:
//
//  main.m
//  Narcissists
//
//  Created by Douglas Johnston on 7/3/17.
//

#import <Foundation/Foundation.h>
#import "NarcissistsMain.h"
#import "MMStopwatchARC.h"

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        NarcissistsMain *nm = [NarcissistsMain new];
        
        printf("Enter maximum digits to calculate: ");
        char input[5];
        fgets(input, 5, stdin);
        int digits = atoi(input);
        [nm processNumbersWithDigits:digits];    
    }
    
    return 0;

}
Code:
//
//  NarcissistsMain.h
//  Narcissists
//
//  Created by Douglas Johnston on 7/3/17.
//

#import <Foundation/Foundation.h>
#import "NSMutableArray+shuffle.h"

@interface NarcissistsMain : NSObject

-(void)processNumbersWithDigits:(int)theDigits; 
-(NSArray *) calculatePowers;
-(void) reapAnswersFromBlocks :(NSArray *)inArray;
@end
Code:
//
//  NarcissistsMain.m
//  Narcissists
//
//  Created by Douglas Johnston on 7/3/17.
//

#import "NarcissistsMain.h"

unsigned int blockNumber = 0;

@implementation NarcissistsMain

NSMutableArray *narcissisticNumbersArray;

-(void)processNumbersWithDigits:(int)theDigits {

    narcissisticNumbersArray = [NSMutableArray new];

    
    NSArray *myPowerCacheArray = [self calculatePowers];

    NSUInteger numProcessors = [[NSProcessInfo processInfo] processorCount];

    NSLog(@"There are %lu processors.", (unsigned long) numProcessors);

    unsigned long numDigits = theDigits;

    unsigned long max = powl(10, numDigits) - 1;

    NSLog(@"Calculating all numbers to test...");
 
    NSMutableArray *allNumbers = [NSMutableArray arrayWithCapacity:max];
    
    for (unsigned long i = 1; i <= max;  i++) {
        [allNumbers addObject:[NSNumber numberWithUnsignedLongLong:i]];
    }


    [allNumbers shuffle];
    
    NSLog(@"All %lu numbers shuffled.", (unsigned long)[allNumbers count]);
    
    unsigned long eachArrayCount = [allNumbers count] / numProcessors;
    
    NSMutableArray *arrayOfArrays = [NSMutableArray array];
    
    unsigned long  itemsRemaining = [allNumbers count];
    unsigned long  j = 0;
    
    while(itemsRemaining) {
        NSRange range = NSMakeRange(j, MIN(eachArrayCount, itemsRemaining));
        NSArray *subarray = [allNumbers subarrayWithRange:range];
        [arrayOfArrays addObject:subarray];
        itemsRemaining -= range.length;
        j += range.length;
    }
    
    [allNumbers removeAllObjects];
    allNumbers = NULL;
    
    dispatch_queue_t narcissisticQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t narcissisticGroup = dispatch_group_create();
    
    void (^narcissisticBlock)(NSArray *) = ^(NSArray *numbersToTest) {
    //    unsigned int blockNum = ++blockNumber;
    //        NSLog (@"Block %d starting...", blockNum);
    //
        NSMutableArray *answers = [NSMutableArray new];
        
        NSDecimal sum = [[NSNumber numberWithUnsignedLongLong:0] decimalValue];
        NSComparisonResult result = NSOrderedDescending;

        for (NSNumber *i in numbersToTest) {
            
            NSString *stringValue = [NSString stringWithFormat:@"%llu", [i unsignedLongLongValue]];
            NSMutableArray *digits = [NSMutableArray new];
            unsigned long numDigits = floor(log10l([i unsignedLongLongValue]) + 1);
            for (unsigned long digitIndex = 0; digitIndex <numDigits; digitIndex++) {
                digits[digitIndex] = [NSNumber numberWithChar:[stringValue characterAtIndex:digitIndex]];
            }
            

            NSDecimal val = [i decimalValue];

            for (unsigned long s = 0; s < numDigits; s++) {
                
                NSNumber *digit = [NSNumber numberWithChar:[stringValue characterAtIndex:s]];
                NSDecimal powerLookup = [myPowerCacheArray[[digit unsignedLongLongValue] - '0'][numDigits] decimalValue];
                NSDecimalAdd(&sum, &sum, &powerLookup , NSRoundPlain);
                result = NSDecimalCompare(&val, &sum);
                if (result != NSOrderedDescending)
                    break;
            }
            
            
            if (result == NSOrderedSame) {
                [answers addObject: i];
            }
            sum = [[NSNumber numberWithUnsignedLongLong:0] decimalValue];
        }
        [self reapAnswersFromBlocks:answers];
        //NSLog (@"Block number %d quitting", blockNum);
    };
  
    for (NSArray *a in arrayOfArrays)
        dispatch_group_async(narcissisticGroup, narcissisticQueue, ^{ narcissisticBlock(a); });
    
    
    dispatch_group_wait(narcissisticGroup, DISPATCH_TIME_FOREVER);
    NSLog(@"Array of Count %lu: %@", [narcissisticNumbersArray count],
          [narcissisticNumbersArray sortedArrayUsingSelector:@selector(compare:)]);

}


-(void) reapAnswersFromBlocks :(NSArray *) inArray {
    
    @synchronized (narcissisticNumbersArray) {
        [narcissisticNumbersArray addObjectsFromArray:inArray];
    }
    
}

//Calculate each single digit number to the power of each single digit number and store in array

-(NSArray *) calculatePowers {
    
    NSMutableArray *powerCache = [NSMutableArray new];
    
    for (unsigned short i = 0; i < 10; i++) {
        [powerCache addObject: [NSMutableArray new]];
        for (unsigned short x = 0; x < 10; x++) {
            powerCache[i][x] = [NSNumber numberWithUnsignedInteger:(UInt32)powl(i, x)];
        }
    }
    
    return [NSArray arrayWithArray:powerCache];
}

@end
Code:
//
//  NSMutableArray+shuffle.h
//  Narcissists
//
//  Created by Douglas Johnston on 7/4/17.
//

#import <Foundation/Foundation.h>

@interface NSMutableArray (shuffle)

-(void) shuffle;

@end
Code:
//
//  NSMutableArray+shuffle.m
//  Narcissists
//
//  Created by Douglas Johnston on 7/4/17.
//

#import "NSMutableArray+shuffle.h"

@implementation NSMutableArray (Shuffle)

// Add Fisher-Yates shuffle functionality to NSMutableArray

- (void)shuffle
{
    for (NSUInteger i = self.count; i > 1; i--)
        [self exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random_uniform((u_int32_t)i)];
}
@end
This uses code blocks/Grand Central Dispatch which were developed by Apple, but they were made open source and released for use in other OSes, but I don't know if they've been adopted in generic versions of gcc etc. or not. I've never done POSIX threading, so this is the best way I know how to do it.

Last edited by Laserbeak; 07-07-2017 at 02:46 PM. Reason: Ask how many digits
 
Old 07-07-2017, 07:16 AM   #115
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
I think I've gotten this compiled with at least -O3 and it still seems slow compared to what others have gotten. This is a sample output with 7 digits:

Code:
| => time ./Narcissists 
2017-07-07 07:16:56.404 Narcissists[33389:517588] There are 8 processors.
2017-07-07 07:16:56.405 Narcissists[33389:517588] Calculating all numbers to test...
2017-07-07 07:16:57.987 Narcissists[33389:517588] All 9999999 numbers shuffled.
2017-07-07 07:17:47.472 Narcissists[33389:517588] Array of Count 30: (
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    153,
    370,
    371,
    407,
    1634,
    6688,
    8208,
    9474,
    33286,
    54748,
    91818,
    91819,
    92727,
    93084,
    117651,
    548834,
    1741725,
    4210818,
    4955603,
    9800817,
    9926315
)

real	0m51.102s
user	6m24.146s
sys	0m0.624s
Is it a problem with my code or is Objective-C just slow? You can see the multiprocessor effect since user is much more than real, but the C++ version (I think running on one core) posted earlier seemed to be much faster. Maybe I can take that and use Grand Central Dispatch to make it mutiprocessor too, since it works with C++ as well as C and Objective-C.

Edit:
Note: I have a 2.3 GHz Intel Core i7 in a MacBook Pro (Late 2013)

Last edited by Laserbeak; 07-07-2017 at 08:24 AM.
 
Old 07-07-2017, 08:49 AM   #116
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
I changed it to ask how many digits to calculate.
 
Old 07-07-2017, 09:29 AM   #117
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Now I realize I'm getting extra wrong numbers when compared to the Wolfram site. When calculated with a calculator, they also prove to be wrong.

An example from above is 6688. There are many more, all 4 or more digits.

Last edited by Laserbeak; 07-07-2017 at 09:30 AM.
 
Old 07-07-2017, 12:12 PM   #118
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,207

Rep: Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419Reputation: 5419
I have rewritten the code in c (something similar to post #105). it is now at about 2 sec (instead of 3.4).
Code:
#include <string.h>
#include <stdio.h>

typedef unsigned long long int ulli;

ulli powers[12];
char test[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

char next_comb_rep(unsigned char vec[], int place) {
    if (place == 0) {
        vec[0] ++;
    } else {
        if (next_comb_rep(vec, place-1)) {
            vec[place]++;
            for (int j=0; j<place; j++) {
                vec[j] = vec[place];
            }
        }
    }
    return vec[place] == 10;
}

ulli sum_of_power_of_digit(unsigned char digits[], int power) {
    ulli sum = 0;
    for(int i=0; i<power; ++i) {
        sum += powers[digits[i]];
    }
    return sum;
}

void fill_powers(int base) {
    for (int i=1; i<12; i++) {
         powers[i] = i;
         for(int pow=1; pow<base; ++pow) {
             powers[i] *= i;
         }
    }
}

char del (unsigned char v[], char a, int len) {
    for (int i=0; i<len; i++) {
        if ( v[i] == a ) {
            v[i] = 'X';
            return 0;
        }
    }
    return 1;
}

int main() {
    for(int base=3; base<20; ++base) {
        printf("%i digits:\n", base);
        unsigned char vec[base];
        unsigned char vec1[base];
        memset(vec, 0, base * sizeof(char));
        fill_powers(base);
        while(1){
            if(next_comb_rep(vec, base-1)) {
                break;
            }
            ulli num = sum_of_power_of_digit(vec, base);
            memcpy(vec1, vec, base);

            ulli tmp_num = num;
            int b = base;
            while(tmp_num>0){
                if ( del(vec1, tmp_num % 10, base) != 0 )
                    break;
                tmp_num /= 10;
                b--;
            }
//            if (strncmp((const char *)vec1, (const char *)test, base) != 0)
            if (b != 0)
                 continue;
//            out_vec.emplace_back(num);
            printf("%llu\n", num);
        }
//        std::sort(out_vec.begin(), out_vec.end());
//        for(mpz i : out_vec){
//            cout << i << "\n";
//        }
    }
}
the result is not sorted.
 
Old 07-07-2017, 01:32 PM   #119
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by Laserbeak View Post
Now I realize I'm getting extra wrong numbers when compared to the Wolfram site. When calculated with a calculator, they also prove to be wrong.

An example from above is 6688. There are many more, all 4 or more digits.
At the moment I do not understand your code, which limits my ability to fix anything, but here are two clues:

6688 = 6**4 + 6**4 + 8**4

33286 = 3**5 + 3**5 + 2**5 + 8**5

So it looks like either the sum of powers is omitting the power of the last digit, or perhaps the comparison is ignoring the last digit of the sum. Or, as they say, it could be anything.
 
1 members found this post helpful.
Old 07-07-2017, 02:42 PM   #120
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Thumbs up

Quote:
Originally Posted by Beryllos View Post
At the moment I do not understand your code, which limits my ability to fix anything, but here are two clues:

6688 = 6**4 + 6**4 + 8**4

33286 = 3**5 + 3**5 + 2**5 + 8**5

So it looks like either the sum of powers is omitting the power of the last digit, or perhaps the comparison is ignoring the last digit of the sum. Or, as they say, it could be anything.
Yes, I did notice that. But why is it working with most of the numbers?

Objective-C is pretty easy to read especially if you make the variable names long and descriptive as I usually do except for small scope iterators.

But thank you :up:

I have a headache and can't really examine it carefully now, but I will. I'm sure it's just some stupid error somewhere.

EDIT:

All the Apple stuff is very well documented. Like NSMutableArray at https://developer.apple.com/document...?language=objc

But you have to make sure you have it set to Objective-C not Swift.

Last edited by Laserbeak; 07-07-2017 at 02:58 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing line numbers for code bauer172uw Programming 3 04-13-2006 11:10 PM
C programming - sorting random numbers Gigantor Programming 8 12-05-2005 10:32 PM
Printing numbers from a text file dynamically mrobertson Programming 1 06-28-2005 08:19 AM
printing numbers without using semicolon vijeesh_ep Programming 18 09-08-2004 11:59 AM
printing line numbers? fisheromen1031 Programming 1 07-27-2004 02:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration